【发布时间】:2012-04-18 11:15:57
【问题描述】:
我想要一个 2x2 网格,里面有一个按钮。这只是 ICS,所以我尝试使用给定的新 GridLayout。
这是我的布局的 XML:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/favorites_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:rowCount="2"
android:columnCount="2">
<Button
android:text="Cell 0"
android:layout_row="0"
android:layout_column="0"
android:textSize="14dip" />
<Button
android:text="Cell 1"
android:layout_row="0"
android:layout_column="1"
android:textSize="14dip" />
<Button
android:text="Cell 2"
android:layout_row="1"
android:layout_column="0"
android:textSize="14dip" />
<Button
android:text="Cell 3"
android:layout_row="1"
android:layout_column="1"
android:textSize="14dip" />
</GridLayout>
问题是我的观点并没有均匀地延伸到每一行。这会导致我的 GridLayout 右侧出现大量额外空间。
我尝试设置 layout_gravity="fill_horizontal" 但这仅适用于行上的 last 视图。这意味着单元格 1 一直延伸以为单元格 0 提供足够的空间。
关于如何解决这个问题的想法?
【问题讨论】:
-
为什么不在这些 Button 元素上设置特定的 layout_width 和 layout_height 尺寸?
-
为什么不在你的情况下使用 TableLayout?
-
在 Lollipop 中我们现在可以使用 android.support.v7.widget.GridLayout,列数为 3,然后有一个
每个列都可以在没有额外开销的情况下实现所需的效果,除非您专门为 SDK 21 构建,然后您可以使用普通的 GradLayout -
更好的是使用线性布局与 android:layout_width="0dp" 和 android:layout_weight="1"。 developer.android.com/guide/topics/ui/layout/…
标签: android android-layout android-gridview android-gridlayout