有時候在ScrollView中插入ListView、GridView時,列表回默認摺疊,值顯示一行。這時就需要重新計算列表的高度,讓它自動鋪滿。重寫代碼如下:
public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
- 需要特別注意的是,構造函數需要寫兩個,否則會報錯。