为什么ScrollView嵌套的ListView显示的只有一行内容。因为ScrollView默认给children的高度测量模式是UNSPECIFIED,
在ScrollView的onMeasure中,首先调用super.onMeasure,而ScrollView继承自FrameLayout,所以就是调用FrameLayout的onMeasure方法。在该方法中,有如下关键代码,注意measureChildWithMargins方法,该方法被ScrollView覆写了。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
//省略代码
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (mMeasureAllChildren || child.getVisibility() != GONE) {
//测量孩子高度
measureChildWithMargins(child, widthMeasureSpec, 0,
heightMeasureSpec, 0);
//省略代码
}
}
//省略代码
}
ScrollView覆写了measureChildWithMargins方法