如何将 wrap_content 作为高度分配给动态加载的 gridview [英] How to assign wrap_content as height to dynamically loaded gridview

查看:23
本文介绍了如何将 wrap_content 作为高度分配给动态加载的 gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用按钮动态加载 gridview.因此,为此我使用了滚动视图,但是如果我将 wrap_content 作为高度分配给 gridview,则不会显示所有按钮.我不想为 gridview 分配任何静态高度.这是我正在使用的代码:

I am loading gridview dynamically with buttons. So for that I am using scrollview, But if i assign wrap_content as height to gridview all the buttons are not displayed. I dont want to assign any static height for the gridview. This is the code which I am using:

<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>

推荐答案

你需要新建一个类例如

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

also you need to change in your 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

and finally in your java class you need to chance the object GridView to WrappingGridView

这篇关于如何将 wrap_content 作为高度分配给动态加载的 gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆