很难在网格布局中布局视图

时间:2015-08-04 14:27:03

标签: android android-gridlayout

在我的android项目中,我有一个有三列的网格布局,但是当我向网格添加视图时,它没有显示设计器中的第三列。第一列包含文本视图,第二列包含微调框,第三列包含图像视图。每个视图的宽度设置为“Wrap_content”。但是第二列中的微调器正在占用剩余的空间并将第三列推出界限。这是我的xml

<android.support.v7.widget.GridLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numColumns="3"
            app:columnCount="3"
            android:columnWidth="auto_fit"
            app:columnOrderPreserved="true"
            app:orientation="horizontal"
            app:rowOrderPreserved="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/title"
                android:id="@+id/textView2"
                app:layout_gravity="center"
                app:layout_column="0"
                app:layout_row="0"/>

            <Spinner
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_gravity="center"
                android:id="@+id/spinner"
                app:layout_column="1"
                app:layout_row="0"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_gravity="center"
                android:id="@+id/imageView"
                android:background="@drawable/ic_astrick"
                app:layout_column="2"
                app:layout_row="0"/>
        </android.support.v7.widget.GridLayout>`

我尝试过给所有视图赋予布局权重,但没有用。我已经附加了布局的预览。图像视图的位置标记为红色

enter image description here

任何人都可以告诉我一种在网格布局中正确排列视图的方法。

1 个答案:

答案 0 :(得分:2)

从API 21开始,&#39; weight&#39;是在GridLayout中引入的。 documetation link

  

要使列拉伸,请确保其中的所有组件   定义重量或重力。为防止色谱柱拉伸,   确保列中的某个组件未定义a   重量或重力。

其他答案的例子:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:grid="http://schemas.android.com/apk/res-auto"

    android:id="@+id/choice_grid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:padding="4dp"

    grid:alignmentMode="alignBounds"
    grid:columnCount="2"
    grid:rowOrderPreserved="false"
    grid:useDefaultMargins="true">

    <TextView
        android:layout_width="0dp"
        android:layout_height="100dp"
        grid:layout_columnWeight="1"
        grid:layout_gravity="fill_horizontal"
        android:gravity="center"
        android:background="#FF33B5E5"
        android:text="Tile1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="100dp"
        grid:layout_columnWeight="1"
        grid:layout_gravity="fill_horizontal"
        android:gravity="center"
        android:background="#FF33B5E5"
        android:text="Tile2" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="100dp"
        grid:layout_columnWeight="1"
        grid:layout_gravity="fill_horizontal"
        android:gravity="center"
        android:background="#FF33B5E5"
        android:text="Tile3" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="100dp"
        grid:layout_columnWeight="1"
        grid:layout_gravity="fill_horizontal"
        android:gravity="center"
        android:background="#FF33B5E5"
        android:text="Tile4" />

</android.support.v7.widget.GridLayout>