如何将wrap_content分配为动态加载的gridview的高度

5
我正在使用按钮动态加载gridview。为此,我使用scrollview,但是如果我将wrap_content分配为gridview的高度,则无法显示所有按钮。 我不想为gridview指定任何静态高度。 这是我正在使用的代码:
<ScrollView
                 xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" >

              <LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical"
                  android:paddingLeft="5dip"
                  android:paddingRight="5dip" >

                  <GridView
                      android:id="@+id/gridviewtable"
                      android:layout_width="fill_parent"
                      android:layout_height=wrap_content"
                      android:horizontalSpacing="10dp"
                      android:numColumns="4"
                      android:verticalSpacing="10dp" >
                  </GridView>

              </LinearLayout>
          </ScrollView>
5个回答

11

你需要创建一个新类,例如

public class WrappingGridView extends GridView {

public WrappingGridView(Context context) {
    super(context);
}

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

public WrappingGridView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightSpec = heightMeasureSpec;
    if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
        // The great Android "hackatlon", the love, the magic.
        // The two leftmost bits in the height measure spec have
        // a special meaning, hence we can't use them to describe height.
        heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    }
    super.onMeasure(widthMeasureSpec, heightSpec);
}

}

你还需要在XML中进行更改

<com.your.project.WrappingGridView
                android:id="@+id/gridviewtable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:horizontalSpacing="10dp"
                android:numColumns="4"
                android:verticalSpacing="10dp"/>

最后,在您的Java类中,您需要将对象GridView更改为WrappingGridView


网格中的项目必须设置固定高度,否则它将无法正常工作。 - Renetik
你可以使用 View.MEASURED_SIZE_MASK 代替 Integer.MAX_VALUE >> 2; - Aorlinn
谢谢!经典的谷歌员工和他们半成品的API和组件——即使过了5年也没有修复。他们的GridView和ViewPager一样有缺陷,因为它们太糟糕了,无法在通过适配器以编程方式填充数据时调整自己的组件大小,所以它们不能与wrap_content一起使用...天哪 - qkx

2

尝试这个

int totalHeight = (count / <number of items in a row>);

    if (count % <number of items in a row> != 0) {
        totalHeight++;
    }

    ViewGroup.LayoutParams params = gridview.getLayoutParams();

    params.height = <height of a row> * totalHeight;

    gridview.setLayoutParams(params);
    gridview.requestLayout();

1
这个答案的变体是唯一对我有效的东西! - Luke Allison

-1
这是许多之前回答的拼凑,但目前为止这是唯一对我有用的东西:
private static void resizeGridView(GridView gridView, int rows) {
    int measuredHeight = gridView.getMeasuredHeight();
    ViewGroup.LayoutParams params = gridView.getLayoutParams();
    params.height = measuredHeight * rows;
    gridView.setLayoutParams(params);
    gridView.requestLayout();
}

在将要显示到网格视图的内容更新后,请调用此函数。

-1

android:layout_height="wrap_content" 不能用于 AdapterView 的子类(例如 ListView 和 GridView)。GridView 必须获取每一行的高度才能计算自己的高度。

移除 ScrollViewLinearLayout,你不需要它们。GridView 已经内置了自己的滚动逻辑。


如何动态地为GridView分配layout_params? - Ankita Deshmukh
你为什么需要那个? - FD_
由于我正在动态加载内容,因此需要动态分配高度。 - Ankita Deshmukh
GridView的高度会自动增长,为什么你要改变任何东西呢? - FD_
这是不同的布局。你为什么需要它们?我只想知道如何为GridView设置LayoutParams。 - Ankita Deshmukh
显示剩余7条评论

-1

这个或类似的代码应该可以解决问题(我记得的代码)

myImageView.setLayoutParams(new ImageView.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, (LinearLayout.LayoutParams.WRAP_CONTENT));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接