简单的方式做动态,但广场布局 [英] Simple way to do dynamic but square layout
问题描述
我使用一个GridView显示一串的观点有本质LinearLayouts。我希望LinearLayouts全部是正方形,但我也希望他们能够动态调整 - 也就是说,有两列,我想LinearLayouts以取决于屏幕的大小舒展,但仍多。有没有办法通过XML布局要做到这一点还是我以编程方式设置高度和宽度?
I'm using a GridView to display a bunch of views which are essentially LinearLayouts. I want the LinearLayouts to all be square, but I also want them to be dynamically sized--that is, there are two columns and I want the LinearLayouts to stretch depending on the size of the screen but remain square. Is there a way to do this through the xml layout or do I have to set the heights and widths programmatically?
推荐答案
一个整洁的解决方案,方GridView的项目是延长RelativeLayout的或LinearLayout中,并覆盖onMeasure像这样:
A neat solution for square GridView items is to extend RelativeLayout or LinearLayout and override onMeasure like so:
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
这篇关于简单的方式做动态,但广场布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!