设置网格布局编程的孩子ROWSPAN或合并单元格? [英] Set rowSpan or colSpan of a child of a GridLayout programmatically?

查看:366
本文介绍了设置网格布局编程的孩子ROWSPAN或合并单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5列3行的网格布局。现在我可以插入任意的孩子的意见,这是伟大的。更妙的是,我可以为它跨越到2列(在同ROWSPAN)分配columnSpan = 2的一些项目。

I have a GridLayout with 5 columns and 3 rows. Now I can insert arbitrary child views, which is great. Even better is, that I can assign columnSpan=2 to some item in order to span it to 2 columns (the same with rowSpan).

现在的问题是,我不能指定ROWSPAN或columnSpan编程方式(即在运行时)。一些搜索建议是这样的:

The problem now is, that I cannot assign rowSpan or columnSpan programmatically (i.e. at runtime). Some search suggested something like this:

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

不过,我不太明白什么意思规范的参数(启动和大小)。该文档也挺可怜的,在这一点上。

But I don't quite understand what the parameters of spec mean (start and size). The documentation is also quite poor at this point.

任何帮助是非常AP preciated!

Any help is highly appreciated!

细则The_Unknown

Bye The_Unknown

推荐答案

好了,我花了一些时间搞清楚什么是怎么回事。好吧,我没有找到任何工作方式设置columnSpan或ROWSPAN在运行时。

OK, I spent some hours figuring out what's going on here. Well, I didn't find any working way to set columnSpan or rowSpan at runtime.

不过,我发现了一个解决方案,工程(至少对我来说):

But I found a solution that works (at least for me):

private LinearLayout addNewSpannedView(Integer resourceId, ViewGroup rootElement) {
    return (LinearLayout) ((ViewGroup) getLayoutInflater().inflate(resourceId, rootElement, true)).getChildAt(rootElement.getChildCount() - 1);
}
// set columnSpan depending on some logic (gridLayout is the layout to add the view's to -> in my case these are LinearLayouts)
shape = addNewSpannedView(columnSpan == 1 ? R.layout.grid_ll_col_span_1 : R.layout.grid_ll_col_span_2, gridLayout);

grid_ll_col_span_2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/shapeWidth"
    android:layout_height="wrap_content"
    android:layout_columnSpan="2"/>

提示:这是非常重要的,你设置的宽度和高度属性,之前膨胀()将着眼于根元素(即父元素)

Hint: It's very important, that you set the width and height attribute, before inflate() adds the view to the root element (i.e. the parent element).

我希望有人可以使用这个; - )

I hope, someone can use this ;-)

这篇关于设置网格布局编程的孩子ROWSPAN或合并单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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