2012-09-15 84 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