1.ScrollView里嵌套ListView时,只显示一个Item
自定义ListView,重写onMeasure方法,在xml布局文件中直接引用即可
public class WrapContentListView extends ListView { public WrapContentListView(Context context) { super(context); } public WrapContentListView(Context context, AttributeSet attrs) { super(context, attrs); } public WrapContentListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public WrapContentListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2, // 设计一个较大的值和AT_MOST模式 MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, newHeightMeasureSpec);//再调用原方法测量 }
2.ScrollView写ListView、GridView时会定位不到页面顶部
在ScrollView父布局中加入
android:focusable="true" android:focusableInTouchMode="true"
3.ScrollView只能放入一个子布局,子布局可以加载多个布局
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> //里面写布局 </LinearLayout> </ScrollView>