2013-04-09 126 views
0

我是Android開發的小菜鳥,我在構建佈局時遇到問題。我有一個包含textview和包含兩個複選框的線性佈局的相對佈局。我希望textView顯示在左側,線性佈局顯示在線性佈局的右側與邊緣齊平。目前,textview和linearlayout顯示在彼此之上。任何幫助是極大的讚賞。爲什麼alignParentRight不能正常工作?

<RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingLeft="5dp" 
       android:text="Rotated Shelf:   " 
       android:layout_gravity="center_vertical"/> 

      <LinearLayout 
       android:id="@+id/rotated" 
       android:layout_width="match_parent" 
       android:layout_height="32dp"     
       android:layout_alignParentRight="true">     

       <CheckBox 
        android:id="@+id/rotatedshelfYES" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Yes" 
        android:layout_toLeftOf="@+id/rotatedshelfNO" 
        /> 

       <CheckBox 
        android:id="@+id/rotatedshelfNO" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:paddingRight="75dp" 
        android:text="No" 
        android:layout_alignParentRight="true" /> 
      </LinearLayout> 
     </RelativeLayout> 
+2

部分是你的LinearLayout設置爲'寬:「match_parent」'這意味着它將會是屏幕的全尺寸。嘗試將其更改爲「wrap_content」,該值應該足夠大以適應複選框,該複選框應該有足夠空間向右邊移動。 – FoamyGuy 2013-04-09 13:57:13

回答

1

嘗試這樣你textview寫在linear-layout並設置android:layout_weight=""它的LinearLayout工作不錯

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:id="@+id/rotated" 
     android:layout_width="match_parent" 
     android:layout_height="32dp" 
     android:layout_alignParentRight="true" > 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_weight=".5" 
      android:paddingLeft="5dp" 
      android:text="Rotated Shelf:   " /> 

     <CheckBox 
      android:id="@+id/rotatedshelfYES" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight=".25" 
      android:text="Yes" /> 

     <CheckBox 
      android:id="@+id/rotatedshelfNO" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight=".25" 
      android:text="No" /> 
    </LinearLayout> 
</RelativeLayout> 
0

添加android:layout_toRightOf="@+id/textView2"

0

如果你要調整你的TextView的右邊緣與LinearLayout的左邊緣(即LinearLayout左邊的TextView)應該使用

android:layout_toLeftOf="@+id/rotated" 

在您的TextView屬性中。

0

使用你必須設置android:layout_width在線性佈局等於wrap_content不match_parent因爲match_parent用於當我們想要等於寬度/高度作爲部件/容器的父母和wrap_content名稱建議只需要空間僅用於寫視圖/容器的標題,不需要額外的空間。你的問題的