Android ScrollView不满一屏的时候默认填满屏幕

 <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可以滑动。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值