gpt4 book ai didi

android - 以编程方式设置 GridLayout 子级的 rowSpan 或 colSpan?

转载 作者:IT老高 更新时间:2023-10-28 21:54:14 25 4
gpt4 key购买 nike

我有一个 5 列和 3 行的 GridLayout。现在我可以插入任意 subview ,这很棒。更好的是,我可以将 columnSpan=2 分配给某个项目,以便将其跨越到 2 列(与 rowSpan 相同)。

现在的问题是,我无法以编程方式(即在运行时)分配 rowSpan 或 columnSpan。一些搜索建议是这样的:

layoutParams.columnSpec = GridLayout.spec(0, columnSpan);

但我不太明白规范的参数是什么意思(开始和大小)。在这一点上,文档也很差。

非常感谢任何帮助!

最佳答案

    GridLayout gridLayout = (GridLayout)findViewById(R.id.tableGrid);

gridLayout.removeAllViews();

int total = 12;
int column = 5;
int row = total / column;
gridLayout.setColumnCount(column);
gridLayout.setRowCount(row + 1);
for(int i =0, c = 0, r = 0; i < total; i++, c++)
{
if(c == column)
{
c = 0;
r++;
}
ImageView oImageView = new ImageView(this);
oImageView.setImageResource(R.drawable.ic_launcher);
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.height = LayoutParams.WRAP_CONTENT;
param.width = LayoutParams.WRAP_CONTENT;
param.rightMargin = 5;
param.topMargin = 5;
param.setGravity(Gravity.CENTER);
param.columnSpec = GridLayout.spec(c);
param.rowSpec = GridLayout.spec(r);
oImageView.setLayoutParams (param);
gridLayout.addView(oImageView);
}

 have look on this image - you can change number of rows and column as you need

关于android - 以编程方式设置 GridLayout 子级的 rowSpan 或 colSpan?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13532084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com