ScrollView 包含一个ViewGroup, 在ViewGroup中填充内容,超过一屏就滚动。
但是发现一个问题,在内容不超过一屏的情况下,设置子内容高度为:match_parent ,并不能起到布满屏幕的作用。并且编辑器还提示子布局建议用
wrap_content.
布局代码如下:
<ScrollView
android:id="@+id/touch_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/red">
//具体内容控件
.......
</LinearLayout>
</ScrollView>
效果如下图:
查看ScrollView 源码看到:mFillViewport 参数为false时直接子视图退出测量,只有设置为true时会进行下面的设置。子布局会填充为父布局(ScorllView的macth_parent)
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
**//设置 mFillViewport**
if (!mFillViewport) {
return;
}
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode == MeasureSpec.UNSPECIFIED) {
return;
}
if (getChildCount() > 0) {
final View child = getChildAt(0);
final int widthPadding;
final int heightPadding;
final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
final FrameLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (targetSdkVersion >= VERSION_CODES.M) {
widthPadding = mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin;
heightPadding = mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin;
} else {
widthPadding = mPaddingLeft + mPaddingRight;
heightPadding = mPaddingTop + mPaddingBottom;
}
final int desiredHeight = getMeasuredHeight() - heightPadding;
// 如果子 view 高度小于 父 view 高度,那么需要重新设定高度
if (child.getMeasuredHeight() < desiredHeight) {
final int childWidthMeasureSpec = getChildMeasureSpec(
widthMeasureSpec, widthPadding, lp.width);
// 这里生成 MeasureSpec 传入的是 父控件也就是(ScorllView的高度),并且用的是 MeasureSpec.EXACTLY
final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
desiredHeight, MeasureSpec.EXACTLY);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
}
}
所以修改后的xml布局如下:
<ScrollView
android:id="@+id/touch_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true” >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/red">
//具体内容控件
.......
</LinearLayout>
</ScrollView>
效果如下
**
备注:这样处理的话,如果包裹的子布局内容不满一屏的情况下,布局也还是会占满整个屏幕。对于一些特殊情况可能要特殊处理
**