Android:如何在xml中平衡GridLayout

时间:2014-11-01 03:53:08

标签: android android-layout android-linearlayout android-gridview

我想要一个带有页眉和页脚的布局以及四个图片(ImageView),它们在中心的两行和两列中具有相同的大小。我不想使用GridView。

这就是我想到的:

Header and footer with block of four ImageViews in center (they are currently buttons)

GridLayout元素随着大小走自己的路,不会缩小到周围元素强加的大小。它们要么变得太大而要离开页面,要么被以下的LinearLayout裁剪掉。这是我的意思的一个例子:

Screenshot on Nexus 5

我想你需要看xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_column="1"
            android:layout_columnSpan="2"
            android:layout_gravity="fill_vertical|fill_horizontal"
            android:layout_row="0"
            android:layout_weight="1"
            android:columnCount="2"
            android:weightSum="4"
            android:rowCount="2" >

    <Button
        android:id="@+id/button2"
        android:layout_column="0"
        android:layout_row="0"
        android:layout_gravity="fill_vertical|fill_horizontal"
    android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button13"
        android:layout_column="1"
        android:layout_row="0"
        android:layout_gravity="fill_vertical|fill_horizontal"
    android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button14"
        android:layout_column="0"
        android:layout_row="1"
        android:layout_gravity="fill_vertical|fill_horizontal"
    android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button15"
        android:layout_column="1"
        android:layout_row="1"
        android:layout_gravity="fill_vertical|fill_horizontal"
    android:layout_weight="1"
        android:text="Button" />

        </GridLayout>
    </LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >
</LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </LinearLayout>

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

我想:

  • 页眉和页脚
  • 四张相同尺寸的中心照片
  • 四幅图片块周围的左右边距。

也许我采取了错误的做法,如果有必要,请随时向我发送新的方向。

1 个答案:

答案 0 :(得分:1)

如果没有GridView,您可以尝试一下:

它完美无缺。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="fill_vertical|fill_horizontal"
                    android:layout_row="0"
                    android:layout_weight="1"
                    android:text="Button" />

                <Button
                    android:id="@+id/button13"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="fill_vertical|fill_horizontal"
                    android:layout_row="0"
                    android:layout_weight="1"
                    android:text="Button" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/button14"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="fill_vertical|fill_horizontal"
                    android:layout_weight="1"
                    android:text="Button" />

                <Button
                    android:id="@+id/button15"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="fill_vertical|fill_horizontal"
                    android:layout_weight="1"
                    android:text="Button" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >
    </LinearLayout>

</LinearLayout>