Android GridLayout 等列宽
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22413822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android GridLayout Equal Column Widths
提问by Boulder Keith
Is it really not possible to easily force equal column widths in an Android GridLayout? Trying the obvious layout...
真的不可能在 Android GridLayout 中轻松强制相等的列宽吗?尝试明显的布局...
<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.)
... 将最右边的按钮置于一列中央,该列明显比其他三个按钮宽。(玩按钮文本,你很容易得到更糟糕的例子。)
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.
我阅读了关于 GridLayouts 中多余空间分布的 Android 文档,但似乎很明显,在这样的情况下,甚至列宽也经常(甚至可能是典型的)需要,我不得不相信我错过了一些明显/容易的东西。
回答by Al Polden
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>
回答by kissed
Just use column weight for GridLayout's children:
只需为 GridLayout 的孩子使用列权重:
android:layout_width="0dp"
android:layout_columnWeight="1"
Rememberusing support library if your project's MinSdk < API 21:
如果您的项目的 MinSdk < API 21,请记住使用支持库:
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:gridlayout-v7:27.1.1'
You should use android.support.v7.widget.GridLayout instead of GridLayout. And don't forget using appnamespace when using support library:
您应该使用 android.support.v7.widget.GridLayout 而不是 GridLayout。并且在使用支持库时不要忘记使用app命名空间:
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="0dp"
app:layout_columnWeight="1"
UPD:
更新:
If you make your GridLayout programmatically, use GridLayout.Spec. For example:
如果您以编程方式制作 GridLayout,请使用 GridLayout.Spec。例如:
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams(
GridLayout.spec(GridLayout.UNDEFINED, 1f),
GridLayout.spec(GridLayout.UNDEFINED, 1f)
);
layoutParams.width = 0;
yourChildView.setLayoutParams(layoutParams);
yourGridLayout.addView(yourChildView);
(for every child view)
(对于每个子视图)