android ScrollView fillViewport属性

为了屏幕适配,包含多元素的布局一般都会使用ScrollView ,以便小屏手机滑动查看,但是在大屏手机上内容全部加载,导致下方空白

我们希望最后的Button是置底的,同时是可以跟随滑动的

例如定义布局xml如下:

  1. <span style=“font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;”><?xml version=“1.0” encoding=“utf-8”?>  
  2. <ScrollView xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent” >  
  5.     <LinearLayout  
  6.         android:layout_width=“match_parent”  
  7.         android:layout_height=“match_parent”  
  8.         android:orientation=“vertical”>  
  9.         <LinearLayout   
  10.             android:layout_height=“350dp”  
  11.             android:layout_width=“match_parent”  
  12.             android:orientation=“vertical”  
  13.             android:background=“#ffffff00”>  
  14.             <TextView   
  15.                 android:layout_width=“fill_parent”  
  16.                 android:layout_height=“wrap_content”  
  17.                 android:text=“content”/>  
  18.         </LinearLayout>  
  19.         <LinearLayout   
  20.             android:layout_height=“match_parent”  
  21.             android:layout_width=“match_parent”  
  22.             android:orientation=“horizontal”>  
  23.         <Button   
  24.                 android:layout_width=“match_parent”  
  25.                 android:layout_height=“45dp”  
  26.                 android:layout_gravity=“bottom”/>  
  27.         </LinearLayout>  
  28.     </LinearLayout>  
  29. </ScrollView></span>  
<span style="font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;"><?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout 
            android:layout_height="350dp"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:background="#ffffff00">
            <TextView 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="content"/>
        </LinearLayout>
        <LinearLayout 
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="horizontal">
        <Button 
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_gravity="bottom"/>
        </LinearLayout>
    </LinearLayout>
</ScrollView></span>

运行效果是这个样子:


原因就是当子布局高度小于ScrollView的高度时,定义子布局match_parent或者fill_parent不起作用,因此设置layout_gravity也不起作用


在scrollview里添加属性android:fillViewport=”true” 就可以了,使得子布局高度和scrollview一样,而当子布局高度超过scrollview的高度时,这个属性就没有意义了

Romain Guy write a little info about a ScrollView attribute that is missing  from documentation :android:fillViewport=”true” .

It must be set to ScrollView and has the following efect : when set to true, this attribute causes the scroll view’s child to expand to the height of the ScrollView if needed. When the child is taller than the ScrollView, the attribute has no effect.


添加属性后的效果图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值