Android 中 GridView嵌套在ScrollView里只有一行的解决方法

Android 中 GridView嵌套在ScrollView里只有一行的解决方法,第1张

概述在做android项目中遇到一个bug,GridView嵌套在ScrollView里只有一行的问题。下面小编在网上找到了解决方法,具体方法如下所示:

在做androID项目中遇到一个BUG,GrIDVIEw嵌套在ScrollVIEw里只有一行的问题。下面小编在网上找到了解决方法,具体方法如下所示:

方法一:就是上面说的通过计算出来ListVIEw或者GrIDVIEw中的子列高度和 进行显示:

public voID setListVIEwHeightBasedOnChildren(ListVIEw ListVIEw) { listadapter listadapter = ListVIEw.getAdapter(); if (listadapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listadapter.getCount(); i++) { VIEw ListItem = listadapter.getVIEw(i,null,ListVIEw); ListItem.measure(0,0); totalHeight += ListItem.getMeasuredHeight(); } VIEwGroup.LayoutParams params = ListVIEw.getLayoutParams(); params.height = totalHeight + (ListVIEw.getdivIDerHeight() * (listadapter.getCount() - 1)); ((marginLayoutParams)params).setmargins(15,15,15); ListVIEw.setLayoutParams(params);}

方法二:重写GrIDVIEw和ListVIEw的onMeasure方法,直接给它一个足够大的高度:

重写ListVIEw:

public class MyListVIEw extends ListVIEw { public MyListVIEw(Context context) { // Todo auto-generated method stub super(context); } public MyListVIEw(Context context,AttributeSet attrs) { // Todo auto-generated method stub super(context,attrs); } public MyListVIEw(Context context,AttributeSet attrs,int defStyle) { // Todo auto-generated method stub super(context,attrs,defStyle); } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { // Todo auto-generated method stub int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST); super.onMeasure(wIDthMeasureSpec,expandSpec); } }

重写GrIDVIEw:

public class MyGrIDVIEw extends GrIDVIEw{ public MyGrIDVIEw(Context context,AttributeSet attrs) { super(context,attrs); } public MyGrIDVIEw(Context context) { super(context); } public MyGrIDVIEw(Context context,int defStyle) { super(context,defStyle); } @OverrIDe public voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,expandSpec); } }

xml中的布局:

<com.xxx.MyGrIDVIEw androID:ID="@+ID/mygrIDvIEw" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:gravity="center" androID:horizontalSpacing="5dp" androID:numColumns="4" androID:stretchMode="columnWIDth" androID:verticalSpacing="6dp" />

以上所述是小编给大家介绍的AndroID 中 GrIDVIEw嵌套在ScrollVIEw里只有一行的解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android 中 GridView嵌套在ScrollView里只有一行的解决方法全部内容,希望文章能够帮你解决Android 中 GridView嵌套在ScrollView里只有一行的解决方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1148011.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存