以编程方式在 GridLayout 中设置 layout_column 和 layout_row [英] Set layout_column and layout_row in GridLayout programmatically

查看:31
本文介绍了以编程方式在 GridLayout 中设置 layout_column 和 layout_row的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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_columnlayout_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_columnlayout_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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆