我成功创建了一个 SquareRelativeLayout。然后我将此 CustomLayout 用作 GridLayout 的一个网格项。我现在有 5 个项目(但它可以是可变的)。对于第一行,一切都很好。我的物品尺寸为 400*400,但第二行的尺寸为 400*429。我认为我的问题来自上面,确实我使用了 LinearLayout 并且我的 GridLayout 的高度为 layout:weight 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 ?我错过了一个属性吗?如果你能帮助我,请不要犹豫:)