Android布局动作栏按钮

时间:2011-10-06 03:17:27

标签: android layout button

对于这个有点无聊的问题感到抱歉,但是我试图在“操作栏”上布置按钮,并且使用“layout_alignRight”之类的东西遇到了麻烦。我想要实现的目标如下:

| [button1] _ _ _空格_ _ _ [button2] [button3] [button4] |

我有一个RelativeLayout封闭了四个按钮所在的位置,我尝试了以下代码无效:

<RelativeLayout android:id="@+id/actionBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#666666" >


    <Button android:id="@+id/button1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignLeft="@id/actionBar"
        android:background="@drawable/buttonImg" />


    <Button android:id="@+id/button4"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignRight="@id/actionBar"
        android:background="@drawable/buttonImg" />


    <Button android:id="@+id/button3"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toLeftOf="@id/button2"
        android:background="@drawable/buttonImg" />


    <Button android:id="@+id/button2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toLeftOf="@id/button3"
        android:background="@drawable/buttonImg" />                 


</RelativeLayout>

按钮似乎总是伸出来,或者所有人都堆积在一起。有谁知道如何实现我正在寻找的间距?

感谢!!!

1 个答案:

答案 0 :(得分:1)

只使用相对布局,你必须锚定最左边的按钮,然后是最右边的按钮,然后添加最右边按钮左边的另外两个按钮:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <Button android:id="@+id/b1" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:text="Button1"></Button>
    <Button android:id="@+id/b4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:text="Button4"></Button>
    <Button android:id="@+id/b3"
      android:layout_width="wrap_content"      
      android:layout_height="wrap_content"
      android:layout_toLeftOf="@id/b4"
      android:text="Button3"></Button>
    <Button android:id="@+id/b2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_toLeftOf="@id/b3"
      android:text="Button2"></Button>
</RelativeLayout>