盛夏的果实伴奏,吉林省全城热恋,龙飞九宵
前些日子在开发中用到了需要scrollview嵌套gridview的情况,由于这两款控件都自带滚动条,当他们碰到一起的时候便会出问题,即gridview会显示不全。 找到大家的通用解决办法。记录一下。
解决办法,自定义一个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, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } @override public void onmeasure(int widthmeasurespec, int heightmeasurespec) { int expandspec = measurespec.makemeasurespec( integer.max_value >> 2, measurespec.at_most); super.onmeasure(widthmeasurespec, expandspec); } }
该自定义控件只是重写了gridview的onmeasure方法,使其不会出现滚动条,scrollview嵌套listview也是同样的道理,不再赘述。
xml布局代码
<scrollview android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/scroll_content"> <com.yourclass.mygridview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/grid_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:numcolumns="auto_fit" android:horizontalspacing="1dip" android:verticalspacing="1dip" android:columnwidth="150dip" android:stretchmode="columnwidth" android:gravity="center"> </com.yourclass.mygridview> </scrollview>
java调用代码
mygridview gridview = (mygridview) findviewbyid(r.id.grid_view); gridview.setadapter(new imageadapter());
以上所述是小编给大家介绍的android中scrollview嵌套gridview的解决办法,希望对大家有所帮助
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android apk 项目一键打包并上传到蒲公英的实现方法
Android 自定义LineLayout实现满屏任意拖动功能的示例代码
android 限制某个操作每天只能操作指定的次数(示例代码详解)
Android 集成 google 登录并获取性别等隐私信息的实现代码
网友评论