【发布时间】:2022-03-16 00:06:38
【问题描述】:
我想用四个具有相同宽度和高度的按钮来填充整个屏幕,所以我认为网格布局是个好主意:
<GridLayout
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_columnWeight="1"
android:layout_rowWeight="0"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_columnWeight="1"
android:layout_rowWeight="0"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
/>
</GridLayout>
不幸的是,我的结果如下所示:
不明白为什么第二排的高度这么大?
我认为网格布局中的每个单元格都有相同的宽度和高度!
【问题讨论】:
-
更新您的图片链接。打不开
-
显然是因为rowWeight不同
-
尝试将所有 rowWeights 设置为 1
-
是的,你说得对,但我真的不明白为什么它们现在不应该排成一行?我以为我需要两行...
-
事实上,正如您所说,现在它可以工作了。这意味着您最终得到了一个完美分割的 2x2 网格。这就是权重的工作原理:如果您希望它们均匀分布,它们必须彼此均匀。就这么容易。不是吗?
标签: android android-layout height width android-gridlayout