Android 在 TableLayout 中均匀地拉伸列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1666685/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:26:26  来源:igfitidea点击:

Android Stretch columns evenly in a TableLayout

android

提问by Drahcir

I am displaying a table of values in my android application, and would like the columns to be distributed evenly in terms of size , instead of sizing according to content.

我在我的 android 应用程序中显示一个值表,并希望列按大小均匀分布,而不是根据内容调整大小。

Been playing around with stretchColumns but couldn't manage to figure out the right combination, Any Ideas?

一直在玩stretchColumns,但无法找出正确的组合,有什么想法吗?

回答by Simon

I had the same problem - I always only entered one number in android:stretchColumns, but you have to enter all columns that should be stretched. So if you have three columns, you have to write:

我遇到了同样的问题 - 我总是只在 中输入一个数字android:stretchColumns,但您必须输入所有应该拉伸的列。所以如果你有三列,你必须写:

android:stretchColumns="0,1,2"

Or write:

或者写:

android:stretchColumns="*"

Then all columns will have the same size. As a reminder, android:stretchColumnsis an attribute for the TableLayoutelement.

然后所有列将具有相同的大小。提醒一下,android:stretchColumnsTableLayout元素的一个属性。

回答by linitbuff

You need to set BOTH android:layout_width="0dip"and android:layout_weight="1"for each view within a table row. I think this works because the weights determine the proportion of the EMPTY SPACE in the row used by the respective views, and since the width of all views is set to 0dip, the whole row is empty space, and hence with equal weights the views are all allocated the same proportion of the WHOLE width.

您需要为表行中的每个视图设置 BOTHandroid:layout_width="0dip"android:layout_weight="1"。我认为这是有效的,因为权重决定了各个视图使用的行中的 EMPTY SPACE 的比例,并且由于所有视图的宽度设置为0dip,整行都是空白空间,因此权重相同的视图都是分配相同比例的整体宽度。

回答by Meriam

Use the android:layout_weightfor the columns

使用android:layout_weight的列

回答by CommonsWare

You control the size of the columns by the size of their contents. If you want them all to be the same size, set that up via android:layout_widthin the appropriate cells.

您可以通过其内容的大小来控制列的大小。如果您希望它们都具有相同的大小,请android:layout_width在适当的单元格中进行设置。

回答by FDIM

Whoever is still looking for solution, for me: setting columns both shrinkable and stretchable did the trick. I had pretty much the same problem as OP.

谁仍在寻找解决方案,对我来说:设置可收缩和可拉伸的列就行了。我遇到了与 OP 几乎相同的问题。