核心解决方案: 重写ListView或者GridView的OnMesure 方法。
代码如下:
public class MyListView extends ListView {
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
GridView
代码如下:
public class MyGridView extends GridView {
private boolean haveScrollbar = true;
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setHaveScrollbar(boolean haveScrollbar) {
this.haveScrollbar = haveScrollbar;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (haveScrollbars == false) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
您可能感兴趣的文章:Android ScrollView 下嵌套 ListView 或 GridView出现问题解决办法Android中ScrollView嵌套GridView显示不全解决方法Android中ScrollView嵌套GridView的解决办法Android 中ScrollView嵌套GridView,ListView的实例Android 中 GridView嵌套在ScrollView里只有一行的解决方法Android开发之机顶盒上gridview和ScrollView的使用详解Android编程开发之ScrollView嵌套GridView的方法ScrollView嵌套ListView及ListView嵌套的高度计算方法Android开发实现ScrollView中嵌套两个ListView的方法Android解决ScrollView下嵌套ListView和GridView中内容显示不全的问题