GridLayout列不正确

时间:2017-04-05 18:49:42

标签: android android-layout grid-layout

我尝试配置2行2列的网格布局。但是这里的每个元素占据了屏幕的整个宽度,而第二列则位于屏幕之外。请在这里建议我做错了什么。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorTheme"
    android:orientation="vertical">

    <GridLayout
        android:id="@+id/moodsGridLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:columnCount="2"
        android:orientation="vertical"
        android:rowCount="2">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="0"
            app:srcCompat="@mipmap/brewtv_logos" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_row="0"
            app:srcCompat="@mipmap/brewtv_logos" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="1"
            app:srcCompat="@mipmap/brewtv_logos" />

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_row="1"
            app:srcCompat="@mipmap/brewtv_logos" />
    </GridLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:2)

试试这个:

用于Gridlayout中的Uniform width列和行:

android:layout_columnWeight="1"    
android:layout_rowWeight="1"

使用此布局:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_white"
    android:orientation="vertical">

    <GridLayout
        android:id="@+id/moodsGridLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView2"
        android:columnCount="2"
        android:orientation="vertical"
        android:rowCount="2">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:src="@mipmap/ic_launcher" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:src="@mipmap/ic_launcher" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_row="1"
            android:layout_rowWeight="1"
            android:src="@mipmap/ic_launcher" />

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_row="1"
            android:layout_rowWeight="1"
            android:src="@mipmap/ic_launcher" />
    </GridLayout>

</LinearLayout>

输出:

enter image description here