在网格布局编程设置layout_column和layout_row [英] Set layout_column and layout_row in GridLayout programmatically
问题描述
我有一个网格布局(不GridView控件),我想补充一些意见具有特殊的行和列INEX。在XML中我可以设置观与
<的TextView
机器人:ID =@ + ID / textView1
机器人:layout_column =2
机器人:layout_row =4
机器人:文本=大文本/>
但我怎么能 layout_column
和 layout_row
编程设置的属性?我想是这样的:
网格布局网格=新的网格布局(getActivity());
grid.setColumn(2);
grid.setRow(4);
grid.addView(新按钮(getActivity());
layout_column
的等效和 layout_row
,与所有布局_...
参数,是可以找到的的LayoutParams
的子类的参数。
在这种情况下,它的 GridLayout.LayoutParams
,我们用它像这样(为一个2x2的网格,最终的行和列的一个子视图,在细胞内中心)
gridLayout.setColumnCount(2);
gridLayout.setRowCount(2);
gridLayout.addView(子视图,新GridLayout.LayoutParams(
GridLayout.spec(1,GridLayout.CENTER),
GridLayout.spec(1,GridLayout.CENTER)));
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" />
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());
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
.
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)));
这篇关于在网格布局编程设置layout_column和layout_row的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!