设置行数和列数的网格布局动态 [英] Setting rowcount and column count for grid layout dynamically

查看:287
本文介绍了设置行数和列数的网格布局动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加入linearlayouts动态网格布局。我想,要显示类似网格视图。是否有可能来计算有多少的线性布局可以放置在一个行和列设置为计数网格布局或者我应该使用GridView控件自动对齐。

I am adding linearlayouts to GridLayout dynamically. I want that to be displayed like grid view. Is it possible to calculate how many linear layouts can be placed in one row and set column count for grid layout or i should use gridview to align them automatically.

推荐答案

如果要计算列计数的基础上的网​​格布局,覆盖宽度 onMeasure 方法。它提供了 widthSpec heightSpec 作为参数,从中可以获取以像素为单位的实际宽度和高度,使用的 MeasureSpec.getSize()。从那里,就算你想多少列根据网​​格布局你刚才发现,并使用 setColumnCount ,使其显示的列数。

If you want to calculate the column count based on the width of the GridLayout, override the onMeasure method. It provides widthSpec and heightSpec as parameters, from which you can get the actual width and height in pixels using MeasureSpec.getSize(). From there, calculate how many columns you'd like to show based on the width of the GridLayout you just found, and use setColumnCount to make it display that number of columns.

这篇关于设置行数和列数的网格布局动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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