//重写gridview的onMeasure方法
public class MyGridView extends GridView{
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandspec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
//重新计算高度
private void setGrideViewHeight(GridView gridView){
ListAdapter adapter = gridView.getAdapter();
if(adapter==null)
return;
int totalHeight=0;
int lineNum=4;
View item = adapter.getView(0, null, gridView);
item.measure(0,0);
totalHeight= item.getMeasuredHeight()*lineNum;
ViewGroup.LayoutParams layoutParams = gridView.getLayoutParams();
layoutParams.height=totalHeight;
((ViewGroup.MarginLayoutParams)layoutParams).setMargins(10,10,10,10);
gridView.setLayoutParams(layoutParams);
}
网友评论