/**
* 自定义GridView,自动适配高度
*
*/
public class YGridView extends GridView {
public YGridView(Context context) {
super(context);
}
public YGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public YGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public YGridView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
重写onMearsure方法,GridView就能根据你的内容的多少而决定它的高度