![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
scrollview嵌套scrollview嵌套gridview出现问题怎么办
1个回答
展开全部
解决办法,自定义一个GridView控件
[java] view plain copy
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布局代码
[html] view plain copy
<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调用代码
[java] view plain copy
MyGridView gridview = (MyGridView) findViewById(R.id.grid_view);
gridview.setAdapter(new ImageAdapter(this));
[java] view plain copy
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布局代码
[html] view plain copy
<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调用代码
[java] view plain copy
MyGridView gridview = (MyGridView) findViewById(R.id.grid_view);
gridview.setAdapter(new ImageAdapter(this));
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询