我会用垂直的LinearLayout
三行与孩子的重量相同,每行都是水平的LinearLayout
有两个体重相同的孩子,这将确保整个区域被填满。对于六个按钮来说,性能应该不是问题。
如果性能是一个问题,您可以将行设置为RelativeLayout
并使用支柱将其分成两半,并据此定位两个孩子。
当我说一个strut,我的意思是:
<View android:id="@+id/strut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"/>
Update:既然你正在尝试LinearLayout
s,以下是处理高度和宽度的方法:
家长LinearLayout
可以有:
android:layout_width="match_parent"
android:layout_height="match_parent"
他们三个LinearLayout
孩子们将有:
android:layout_width="match_parent"
android:layout_height="0dip"
The Button
s 将有:
android:layout_width="0dip"
android:layout_height="match_parent"
正如您所注意到的,我们有0dip
对于应用权重的属性(如果父级是垂直方向,则在高度上;如果父级是水平方向,则在高度上),该属性需要增长以填充空间。
以下是完整的 XML(按钮不包含可绘制对象,因此请随意添加您的):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
And the result: