在平常的Android开发中我们经常会碰到ScrollView嵌套ListView或者是GridView的情况,若按照一般的流程我们会发现在ScrollView中的ListView显示不全的问题,其实我们可以重写ListView的onMeasure(int widthMeasureSpec, int heightMeasureSpec)
方法来解决。
以下是重写ListView的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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也可以通过重写来解决:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class MyGridView extends GridView { public MyGridView (Context context, AttributeSet attrs) { super (context, attrs); } public MyGridView (Context context) { super (context); } public MyGridView (Context context, AttributeSet attrs, int defStyle) { super (context, attrs, defStyle); } @Override public void onMeasure (int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2 , MeasureSpec.AT_MOST); super .onMeasure(widthMeasureSpec, expandSpec); } }