以编程方式在 GridLayout 中设置 layout_column 和 layout_row [英] Set layout_column and layout_row in GridLayout programmatically
问题描述
我有一个 GridLayout(不是 GridView),我想在其中添加一些具有特殊行和列 inex 的视图.在 XML 中,我可以设置视图:
I have a GridLayout (not GridView) where I want to add some views with a special row and column inex. In XML I can set the View with:
<TextView
android:id="@+id/textView1"
android:layout_column="2"
android:layout_row="4"
android:text="Large Text" />
但是如何以编程方式设置属性 layout_column
和 layout_row
?我想要这样的东西:
But how can I set the attributes layout_column
and layout_row
programmatically? I want something like this:
GridLayout grid = new GridLayout(getActivity());
grid.setColumn(2);
grid.setRow(4);
grid.addView(new Button(getActivity());
推荐答案
layout_column
和 layout_row
的等价物,与所有 layout_...
相同code> 参数,可以作为 LayoutParams
的子类的参数找到.
The equivalent of layout_column
and layout_row
, as with all layout_...
parameters, is to be found as a parameter of a subclass of LayoutParams
.
在这种情况下它是 GridLayout.LayoutParams
,我们像这样使用它(对于在最后一行和最后一列带有子视图的 2x2 网格,在单元格内居中):
In this case it's GridLayout.LayoutParams
, and we use it like this (for a 2x2 grid with a subview in the final row and column, centred within the cell):
gridLayout.setColumnCount(2);
gridLayout.setRowCount(2);
gridLayout.addView(subview, new GridLayout.LayoutParams(
GridLayout.spec(1, GridLayout.CENTER),
GridLayout.spec(1, GridLayout.CENTER)));
这篇关于以编程方式在 GridLayout 中设置 layout_column 和 layout_row的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!