尝试使用不同大小的元素创建 GridLayout 时出现问题

Issues trying to create GridLayout with elements of different size

我正在尝试在 Android 中创建静态网格状布局,所以我使用 GridLayout 而不是 GridView

这是我要实现的布局

使用 here 中的代码作为基础。我对在网格内的每个元素上指定 layout_{weight, margin} 也有点困惑,因为我的理解是 rowSpancolSpan 参数应该处理这个问题。这不是不正确吗?

有什么指点吗?

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/GridLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="2"
    tools:context=".MainActivity" >

    <android.support.v7.widget.CardView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/card_view"
        android:layout_column="0"
        android:layout_gravity="fill"
        android:layout_row="0"
        android:layout_columnSpan="1">
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/button3"
            android:text="Button" />
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/card_view1"
        android:layout_column="0"
        android:layout_gravity="fill"
        android:layout_row="1"
        android:layout_columnSpan="1">
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/button1"
            android:text="Button" />
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/card_view3"
        android:layout_column="1"
        android:layout_gravity="fill"
        android:layout_row="1">
        <Button
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/button2"
            android:text="Button" />
    </android.support.v7.widget.CardView>

</GridLayout>

你太接近了,试试这个:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/GridLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="2"
    tools:context=".MainActivity" >

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_column="0"
        android:layout_gravity="fill_horizontal"
        android:layout_row="0"
        android:layout_columnSpan="2">
        <Button
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/button3"
            android:text="Button" />
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:id="@+id/card_view1"
        android:layout_column="0"
        android:layout_row="1"
        android:layout_columnSpan="1"
        android:layout_columnWeight="1">
        <Button
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/button1"
            android:text="Button" />
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:id="@+id/card_view3"
        android:layout_column="1"
        android:layout_row="1"
        android:layout_columnWeight="1">
        <Button
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/button2"
            android:text="Button" />
    </android.support.v7.widget.CardView>

</GridLayout>

您需要对第一项使用 layout_gravity="fill_horizontal",以拉伸整行。并使用 layout_columnWeight 来处理第二项和第三项。