在相对的措施如何指定宽度的Andr​​oid网格布局细胞的高度?网格、宽度、布局、细胞

2023-09-05 10:14:01 作者:旧谈

我试图找出如何GridLayout工作原理,但有一件事我不能从文档弄清楚是怎么或者一个可以控制网格单元的大小。

I'm trying to figure out how GridLayout works, but one thing I can't figure out from the documentation is how or if one can control the size of the grid cells.

说我要两两格,每个单元占据的屏幕房地产的整整25%(半高,半宽) - 我能做到这一点?

Say I want a two by two grid where each cell occupy exactly 25% of the screen real estate (half height, half width) - can I do this?

使用的LinearLayout我会通过嵌套两个水平​​的LinearLayout在一个垂直,然后分配为1 体重以所有元素做到这一点。网格布局不支持权重属性,但。

With LinearLayout I would accomplish this by nesting two horizontal LinearLayout's in one vertical and then assigning a weight of 1 to all elements. GridLayout does not support the weight property though.

推荐答案

它看起来像其设置为你想应该根据文档是相当简单:

it looks like setting it as you'd like should be fairly straightforward according to the documentation:

android:rowCount='2'
android:columnCount='2'

和中设置的儿童

android:layout_columnSpan="1"
android:layout_rowSpan="1"

这基准也提到拉伸:

要prevent从绵延一列,确保列中的组成部分之一不定义重力。

To prevent a column from stretching, ensure that one of the components in the column does not define a gravity.

哪个似乎是一个解决方案,以保持在50:50比率的行和列不调整根据内容

Which would seem like a solution to keep the rows and columns in a 50:50 ratio without resizing according to content