安卓如何对齐两个按钮

4
我有两个并排的按钮。我希望它们大小相同并填满整个屏幕宽度。我一直在尝试以下代码:(如果这很重要,它是相对布局)
<Button android:text="Names" 
android:id="@+id/Button01" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
<Button android:text="Dates" 
android:id="@+id/Button02" 
android:layout_toRightOf="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
1个回答

26

在按钮周围包裹一个LinearLayout?然后如果需要,可以使用padding来调整精确位置。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
<Button android:text="Names" 
android:id="@+id/Button01" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
<Button android:text="Dates" 
android:id="@+id/Button02" 
android:layout_toRightOf="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</Button>
</LinearLayout>

1
这是一个优美的解决方案。完全按照需求工作。 - jbranchaud

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接