xml文件里面ScrollView包裹了一个GridView的时候会造成内容显示不全,因为GridView没有测量出子View需要的高度,导致不能显示完全
所以需要从写onMeasure方法 (个人理解)
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height =MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, height);
}
贴出代码
package com.example.activity.custom;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View.MeasureSpec;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
public class MyGridView extends GridView{
public MyGridView(Context context) {
super(context,null);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs,0);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height =MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, height);
}
}