并排2个按钮 - 安卓布局

luc*_*uca 50 layout user-interface android

如何将2个按钮并排放置,以便它们占据所有宽度,两者之间有一点空间.

我想到了一个水平线性布局,2个子线性布局设置为匹配父级和重量1,每个都包含按钮..是否有更简单的方法?这可以用相对布局来完成吗?

谢谢!

2re*_*d13 105

<LinearLayout 
    android:id="@+id/LinearLayout02" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:layout_alignParentBottom="true">
    <Button 
        android:id="@+id/Button02" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" android:text="Apply">
    </Button>
    <Button 
        android:id="@+id/Button03" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:text="Cancel">
    </Button>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • 这不起作用.此示例中的按钮具有相似的宽度,因为内部文本具有相似的长度.尝试使用完全不同长度的文本(我正在使用"Downloaded"和"All")并查看效果. (3认同)
  • layout_alignParentBottom(第5行)对于LinearLayout无效。 (2认同)

iar*_*oyo 5

如果您希望2个按钮占据所有宽度并且按钮具有相同的宽度,则必须在2个按钮中更改属性:

android:layout_width="wrap_content"android:layout_width="match_parent"

因为如果你有一个带有长文本的按钮而另一个带有短文本的按钮,那么带有长文本的按钮会占用更多的空间.


D.S*_*nap 5

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:text="button1"
    android:id="@+id/button" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:text="button2"
    android:id="@+id/button2" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)