我想要一个2x2的网格,里面有一个按钮.这只是ICS,所以我try 使用新的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右侧产生大量额外空间.

我try 设置layout_gravity="fill_horizontal",但这只适用于行上的last视图.这意味着单元格1会一直延伸,为单元格0提供足够的空间.

如何解决这个问题?

推荐答案

101: Weights are supported as of API 21. See 100 for more details.

在使用GridLayout时有一些限制,下面的引语取自documentation.

"GridLayout不支持权重原则,因为 以权重定义.因此,一般而言,不可能 配置GridLayout以非常重要的方式分配多余空间 多行或多列之间的比例.实现完全控制 行或列中过多的空间分布;使用LinearLayout 子视图以保存关联单元格组中的组件."

下面是一个使用LinearLayout子视图的小示例.(我使用了占据未使用区域的空间视图,并将按钮按到所需位置.)

<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="1"
>
    <TextView
        android:text="2x2 button grid"
        android:textSize="32dip"
        android:layout_gravity="center_horizontal" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:orientation="horizontal">
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:text="Button 2" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
    >
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:text="Button 4" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>
</GridLayout>

Android相关问答推荐

写入排除例外以进行依赖性判断

组成底部导航栏,自定义形状,周围透明

StateFlow集合AsState没有更新他的值Jetpack Compose导航

strings.xml中字符串数组中的占位符

解决失败:Landroidx/compose/runtime/PrimitiveSnapshotStateKt

在 Android 房间迁移中获取上下文

需要在按钮 onclick 上从 string.xml 获取值. @Composable 调用只能在@Composable 函数的上下文中发生

如何从 firebase 实时数据库中检索最后一个值

setContentView() 方法的签名

NFC getNdefMessage 在 Android 13 上写入标签后返回 null

有什么方法可以确定正在使用哪个 Android 手机的麦克风进行录音?

Jetpack compose 未解决的参考错误

组合 - 重新组合图像

房间创建三四表关系

删除一对多关系室 Kotlin 中的所有值

Android Studio:如何添加应用程序质量洞察窗口以查看 Android Studio 中的 Crashlytics 数据?

如何将新的 ComponentActivity 与 ViewBinding 和其他旧的 AppCompatActivity 组件一起使用

Android Compose webview 被拉伸

如何在stroke android drawable中设置渐变

如何使在库范围之外无法访问的接口的具体实现.?