2012-09-15 86 views
2

我有一個線性佈局,裏面有兩個線性佈局。兩個linearlayouts都有2個按鈕。我想將左邊的前兩個按鈕和右邊的另外兩個按鈕對齊。不幸的是,我有四個按鈕(或兩個佈局)並排。佈局重力左右

這是我的代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="5dp" 
     android:paddingBottom="5dp" 
     android:background="@drawable/vat_header_selector" 
     android:orientation="horizontal" > 

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

      <Button 
       android:id="@+id/btn_numbers" 
       android:layout_width="90dp" 
       android:layout_height="30dp" 
       android:textColor="@drawable/vat_btn_header_textcolor_selector" 
       android:background="@drawable/vat_btn_header_selector" 
       android:text="Numbers"> 
      </Button> 
      <Button 
       android:id="@+id/btn_text" 
       android:layout_width="90dp" 
       android:layout_height="30dp" 
       android:textColor="@drawable/vat_btn_header_textcolor_selector" 
       android:background="@drawable/vat_btn_header_selector2" 
       android:text="Words"> 
      </Button> 
     </LinearLayout> 

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

      <Button 
       android:id="@+id/btn_black" 
       android:layout_width="50dp" 
       android:layout_height="30dp" 
       android:textColor="@drawable/vat_btn_header_textcolor_selector" 
       android:background="@drawable/vat_btn_header_selector" 
       android:text="B"> 
      </Button> 
      <Button 
       android:id="@+id/btn_white" 
       android:layout_width="50dp" 
       android:layout_height="30dp" 
       android:textColor="@drawable/vat_btn_header_textcolor_selector" 
       android:background="@drawable/vat_btn_header_selector2" 
       android:text="W"> 
      </Button> 
     </LinearLayout> 

    </LinearLayout> 

這是它的樣子: enter image description here

而這正是我想要的: enter image description here

回答

8

重力leftrightLinearLayout的取向被設定爲水平的(作爲視圖沿X軸置於)不起作用。做你想做的一種方法是插入兩個LinearLayouts之間的空View它們之間創造一個空間:

<View android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 
2

應用以下屬性,第二線性佈局,包含Button BButton W

android:gravity="right" 
android:layout_weight="1" 
+0

感謝$這個工程還可以,但我認爲Luksprog的解決方案是更好的。 – erdomester