<ScrollView
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">
</LinearLayout>
</ScrollView>
Scrollview在部分需求中,我们需要设置他的子布局高度铺满整个屏幕。但是直接设置子布局高度为match_parent或者fill_parent是无效的,需要设置ScrollView的fillViewport为true,这样当子布局内容不满一屏幕的时候就会自动填充,无需滚动就能看到所有内容。
为了确保界面纵向内容在小屏幕手机也可以完全显示,不被挤在一起,通常会配合子布局或子控件的minHeight来使用。
<ScrollView
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:minHeight="200dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
或者
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport ="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:android:minHeight="33dp" />
</LinearLayout>
</ScrollView>
这样达到的效果是:如果屏幕足够大,拉伸高度为自适应(wrap_parent)的布局,可以配合权重(weight)来实现比例缩放铺满屏幕,ScrollView无需滚动。如果屏幕比较小,界面按照实际高度(height & minHeight)展示,ScrollView可以滑动。