GridView行的高度大小不同

时间:2018-01-19 03:56:54

标签: android gridview

在我的应用程序中,我使用GridView,但我的每个单元格的值都不相同,当我将我的值放在GridView中时,它会自动设置所有单元格的大小(我认为它考虑所有单元格的第一个单元格高度),你能告诉我怎么解决这个问题? enter image description here

GridView适配器:

2 个答案:

答案 0 :(得分:1)

您应该尝试在Gridview

中使用自定义查看行项目
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) 
   {
    if (widthMeasureSpec < heightMeasureSpec) 
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    else
        super.onMeasure(heightMeasureSpec, heightMeasureSpec);
  }
}

在GridView行XML

<SquareRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:adjustViewBounds="true"
    android:contentDescription="@string/descr_image"
    android:scaleType="centerCrop" />

</SquareRelativeLayout>

希望这会对你有所帮助。

答案 1 :(得分:0)

我建议请RecyclerViewStaggeredGridLayoutManager一起使用,抛出此布局可以获得相同的布局,

这是可以帮助您的链接: https://android--code.blogspot.in/2015/12/android-recyclerview.html