你可以扩展RelativeLayout类(或者使用LinearLayout)来确保项目的高度与高度相同。
public class GridItem extends RelativeLayout {
public GridItem(Context context) {
super(context);
}
public GridItem(Context context, AttributeSet attr) {
super(context, attr);
}
public GridItem(Context context, AttributeSet attr, int integer) {
super(context, attr, integer);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
}
}
项目布局的父视图应该是GridItem视图,以确保其正常工作。这必须是您在ListAdapter的getView中填充的布局文件。
<?xml version="1.0" encoding="utf-8"?>
<my.packagename.GridItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</my.packagename.GridItem>
将 GridView 的 stretchMode 设置为 columnWidth。这将使所有项目适合指定列数的宽度。新视图将确保它们也具有相同的高度。
<GridView
android:id="@+id/gridList"
android:numColumns="2"
android:stretchMode="columnWidth"
/>