带有SquareRelativeLayout项的GridLayout

时间:2016-03-09 15:59:06

标签: android grid-layout square

我成功创建了SquareRelativeLayout。然后我将此CustomLayout用作GridLayout的一个网格项。我现在有5个项目(但它可以是变量)。对于第一行,一切都很好。我的项目大小为400 * 400但是对于第二行我有400 * 429的大小。我认为我的问题来自上面,实际上我使用的是LinearLayout,而我的GridLayout的布局高度为:权重0.50。这是我目前的布局:

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

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#66000000">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="0dp" ... />

        <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/droparea"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.50"
            grid:columnCount="3"
            grid:orientation="horizontal">

            <SquareRelativeLayout
                android:id="@+id/relativelayout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                grid:layout_columnWeight="1"
                grid:layout_gravity="fill"
                android:background="@drawable/round_layout"
                android:gravity="center_vertical|center_horizontal">
                ...
            </SquareRelativeLayout>

            <SquareRelativeLayout
                android:id="@+id/relativelayout2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                grid:layout_columnWeight="1"
                grid:layout_gravity="fill"
                android:background="@drawable/round_layout"
                android:gravity="center_vertical|center_horizontal">
                ...
            </SquareRelativeLayout>

            <SquareRelativeLayout
                android:id="@+id/relativelayout3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                grid:layout_columnWeight="1"
                grid:layout_gravity="fill"
                android:background="@drawable/round_layout"
                android:gravity="center_vertical|center_horizontal"> 
                ...
            </SquareRelativeLayout>

            <SquareRelativeLayout
                android:id="@+id/relativelayout4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                grid:layout_columnWeight="1"
                grid:layout_gravity="fill"
                android:background="@drawable/round_layout"
                android:gravity="center_vertical|center_horizontal">
                ....
            </SquareRelativeLayout>

            <SquareRelativeLayout
                android:id="@+id/relativelayout5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                grid:layout_columnWeight="1"
                grid:layout_gravity="fill"
                android:background="@drawable/round_layout"
                android:gravity="center_vertical|center_horizontal">
                ...
            </SquareRelativeLayout>

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

        <include layout="@layout/draggable_linearlayout"/>

    </LinearLayout>

</RelativeLayout>

这是我的SquareRelativeLayout的代码:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

public class SquareRelativeLayout extends RelativeLayout {
    public SquareRelativeLayout(Context context) {
        super(context);
    }

    public SquareRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

}

所以我希望能够为GridLayout设置一个可变高度,但是要保持平方项。是否可以让GridLayout填充29dp?我错过了一个属性吗?如果你能帮助我,请不要犹豫:)

1 个答案:

答案 0 :(得分:0)

这很简单,但昨天我没有考虑过。你只需要为GridLayout使用ParentLayout,然后设置GridLayout的layout_height属性wrap_content,就是这样:)