Android GridLayout相等的列宽 [英] Android GridLayout Equal Column Widths
问题描述
在Android GridLayout中真的不可能轻易地强制相等的列宽吗?尝试使用明显的布局...
Is it really not possible to easily force equal column widths in an Android GridLayout? Trying the obvious layout...
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4"
android:rowCount="8"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_gravity="center"
android:text="Button 1" />
<Button
android:id="@+id/button2"
android:layout_gravity="center"
android:text="Button 2" />
<Button
android:id="@+id/button3"
android:layout_gravity="center"
android:text="Button 3" />
<Button
android:id="@+id/button4"
android:layout_gravity="center"
android:text="Button 4" />
</GridLayout>
...使最右边的按钮居中在一列中,该列明显比其他三个按钮宽. (使用按钮文字可以轻松得到更糟糕的示例.)
... leaves the rightmost button centered in a column that is clearly wider than the other three. (Play with the button text and you can easily get worse examples.)
我阅读了有关GridLayouts中多余的空间分布的Android文档,但似乎如此明显,以至于在这种情况下,甚至(通常甚至是通常)都需要列宽,我必须相信我缺少明显的东西/简单.
I read the Android documentation regarding excess space distribution in GridLayouts, but it seems so obvious that even column widths are often (any maybe even typically) wanted in a situation like this that I have to believe I'm missing something obvious/easy.
推荐答案
改用表布局并指定拉伸列.
Use a table layout instead and specify stretch columns.
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewLeaveRemainingDescription"
android:stretchColumns="0,1">
<TableRow>
<Button
android:id="@+id/button1"
android:layout_gravity="center"
android:text="Button 1" />
<Button
android:id="@+id/button3"
android:layout_gravity="center"
android:text="Button 3" />
</TableRow>
<TableRow>
<Button
android:id="@+id/button2"
android:layout_gravity="center"
android:text="Button 2" />
<Button
android:id="@+id/button4"
android:layout_gravity="center"
android:text="Button 4" />
</TableRow>
</TableLayout>
这篇关于Android GridLayout相等的列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!