Gridview高度被削减

我试图在GridView中显示8项。 不幸的是,GridView的高度总是太小,所以它只显示第一行,第二行显示一小部分。

设置android:layout_height="300dp"使它工作。 wrap_ contentfill_parent显然不是。

我的网格视图:

 <GridView android:id="@+id/myId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:horizontalSpacing="2dp" android:isScrollContainer="false" android:numColumns="4" android:stretchMode="columnWidth" android:verticalSpacing="20dp" /> 

我的项目资源:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:minHeight="?android:attr/listPreferredItemHeight" > <ImageView android:id="@+id/appItemIcon" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@android:drawable/ic_dialog_info" android:scaleType="center" /> <TextView android:id="@+id/appItemText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="My long application name" android:gravity="center_horizontal" android:textAppearance="?android:attr/textAppearanceSmall" /> </LinearLayout> 

这个问题似乎与缺乏垂直空间有关。

我能做什么 ?

经过(太多)研究,我偶然发现了Neil Traft的出色答案 。

适应GridView工作已经非常简单。

ExpandableHeightGridView.java:

 package com.example; public class ExpandableHeightGridView extends GridView { boolean expanded = false; public ExpandableHeightGridView(Context context) { super(context); } public ExpandableHeightGridView(Context context, AttributeSet attrs) { super(context, attrs); } public ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public boolean isExpanded() { return expanded; } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // HACK! TAKE THAT ANDROID! if (isExpanded()) { // Calculate entire height by providing a very large height hint. // View.MEASURED_SIZE_MASK represents the largest height possible. int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); ViewGroup.LayoutParams params = getLayoutParams(); params.height = getMeasuredHeight(); } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } public void setExpanded(boolean expanded) { this.expanded = expanded; } } 

将其包含在您的布局中,如下所示:

 <com.example.ExpandableHeightGridView android:id="@+id/myId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:horizontalSpacing="2dp" android:isScrollContainer="false" android:numColumns="4" android:stretchMode="columnWidth" android:verticalSpacing="20dp" /> 

最后,你只需要问它扩大:

 mAppsGrid = (ExpandableHeightGridView) findViewById(R.id.myId); mAppsGrid.setExpanded(true); 

在使用@tacone的答案,并确保它的工作后,我决定尝试缩短代码。 这是我的结果。 PS:这相当于将tacones中布尔值“expanded”的回答始终设置为true。

 public class StaticGridView extends GridView { public StaticGridView(Context context) { super(context); } public StaticGridView(Context context, AttributeSet attrs) { super(context, attrs); } public StaticGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST)); getLayoutParams().height = getMeasuredHeight(); } } 

另一种类似的方法是为一行计算高度,然后使用静态数据(可以将其调整为分页),可以计算出有多less行,并轻松调整GridView高度。

  private void resizeGridView(GridView gridView, int items, int columns) { ViewGroup.LayoutParams params = gridView.getLayoutParams(); int oneRowHeight = gridView.getHeight(); int rows = (int) (items / columns); params.height = oneRowHeight * rows; gridView.setLayoutParams(params); } 

在设置适配器和绘制GridView的时候使用这段代码,否则你将得到height = 0。

 gridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (!gridViewResized) { gridViewResized = true; resizeGridView(gridView, numItems, numColumns); } } }); 

findtacones回答helpfull …所以我把它移植到C#(Xamarin)

 public class ExpandableHeightGridView: GridView { bool _isExpanded = false; public ExpandableHeightGridView(Context context) : base(context) { } public ExpandableHeightGridView(Context context, IAttributeSet attrs) : base(context, attrs) { } public ExpandableHeightGridView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle) { } public bool IsExpanded { get { return _isExpanded; } set { _isExpanded = value; } } protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) { // HACK! TAKE THAT ANDROID! if (IsExpanded) { // Calculate entire height by providing a very large height hint. // View.MEASURED_SIZE_MASK represents the largest height possible. int expandSpec = MeasureSpec.MakeMeasureSpec( View.MeasuredSizeMask, MeasureSpecMode.AtMost); base.OnMeasure(widthMeasureSpec,expandSpec); var layoutParameters = this.LayoutParameters; layoutParameters.Height = this.MeasuredHeight; } else { base.OnMeasure(widthMeasureSpec,heightMeasureSpec); } } } 

只需计算AT_MOST的高度并设置为开启。 这里GridView Scroll将不能工作。 需要显式使用垂直滚动视图。

  @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int heightSpec; if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) { heightSpec = MeasureSpec.makeMeasureSpec( Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); } else { // Any other height should be respected as is. heightSpec = heightMeasureSpec; } super.onMeasure(widthMeasureSpec, heightSpec); }