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

时间:2016-01-21 01:05:31

标签: android android-appcompat android-gridlayout

我尝试在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>

1 个答案:

答案 0 :(得分:2)

你太近了,试试这个:

<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玩第2和第3项。