2013-01-06 52 views
0

當我在RelativeLayout中使用'textA','textB'和'textC'聲明3個TextView時。 textC應該出現在textB下面。但是運行時,textC顯示位置不正確(位於屏幕的左上角)。 我不知道textC可以使用android:layout_below來textB。無法在RelativeLayout中定義TextView位置

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" > 
    <TextView 
     android:id="@+id/textA" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:text="This is TextA : " /> 
    <TextView 
     android:id="@+id/textB" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/textA" 
     android:layout_alignBaseline="@id/textA" 
     android:text="this is textB" /> 
    <TextView 
     android:id="@+id/textC" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@id/textB" 
     android:text="This is TextC" /> 
</RelativeLayout> 
+0

我只知道alignBaseline應該避免這種情況。所以對其他元素使用相對位置就是解決這個問題的方法。 – user1953029

回答

0

只需修改下面爲TextView3

<TextView 
    android:id="@+id/textC" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/textB" 
    android:layout_below="@+id/textA" 
    android:text="This is TextC" /> 
2

您還可以更改

android:layout_below="@id/textA" 

到textC

完整的例子看起來:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" > 
<TextView 
    android:id="@+id/textA" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_alignParentLeft="true" 
    android:text="This is TextA : " /> 
<TextView 
    android:id="@+id/textB" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/textA" 
    android:layout_alignBaseline="@+id/textA" 
    android:text="this is textB" /> 
<TextView 
    android:id="@+id/textC" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textB" 
    android:layout_below="@+id/textA" 
    android:text="This is TextC" /> 

0

它不能使用垂直取向於使用alignBaseline對於其它元件的元件。然後在這種情況下,在'textB'中將alignBaseline更改爲像代碼中的alignTop一樣。

<TextView 
    android:id="@+id/textB" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/textA" 
    android:layout_alignTop="@id/textA" 
    android:text="this is textB" /> 

使用這種方式,它將支持textB的多行。