2014-02-28 118 views
0

使用相对视图,如何在同一行中放置两个文本视图?一个必须粘在左边,另一个粘在右边。相对视图对齐2文本视图,一个左对齐和一个右对齐

如果你是一名Web开发人员,你可以IMAGIN它分别采用float:leftfloat:right到2文本视图


TextView的1:将包含一个事件标题
的TextView 2:将包含事件的时间

我希望TextView 2缩短,因为它只包含一个时间,而TextView1将包含事件的标题

我希望一个相对布局是这个

一个好方法,我想是这样的:
enter image description here

回答

2

尝试......

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main_lay" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:text="@string/hello_world" /> 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 

或者

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="@string/hello_world" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

</LinearLayout> 
+0

我以前曾尝试过您的第一个解决方案,TextView1中的文本与TextView2重叠。您的解决方案2似乎工作! – Krimson

+0

第一个解决方案显然不起作用,因为text和text1没有关系规则。一个'toLeftOf'至少可能会起作用。 – njzk2

0

如果希望每个的宽度等于你可以使用的LinearLayout。

<LinearLayout android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <TextView android:layout_height="wrap_content" 
     android:layout_width="0dp" android:layout_weight="1" /> 
    <TextView android:layout_height="wrap_content" 
     android:layout_width="0dp" android:layout_weight="1" /> 
</LinearLayout> 
+0

我明白了,但我不希望宽度相等。第二个文本视图将只包含一个时间(下午3:00),第一个textView将包含一个事件标题'(在blablabla上的脱口秀)',所以第一个文本视图必须更长 – Krimson

+0

因此, wrap_content为TextViews的layout_width。 –

0

你可以使用一个线性在一些权重各TextView的顶部对齐或使用这样的相对:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main_lay" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/left" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:text="@string/hello_world" /> 

    <TextView 
     android:id="@+id/right" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="left" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 

但是,我更喜欢林耳内与两个文本的相对位置。

相关问题