ScrollView is one of Android’s most commonly used widget and is also one of the easiest to use. When something is too big to fit on screen, drop it inside a ScrollView and you’re done. You can’t even get it wrong since a ScrollView accepts only one child at a time. There is, however, one use case a bit trickier to get right; unless you’ve carefully read the documentation.

Let’s imagine that your application needs to display a piece of text and a couple of buttons. The length of the text can vary and be longer or shorter than the screen. You want to put the text inside a scroll view and you want the buttons to scroll along with the text, probably to encourage the user to read the text before clicking any of the button. Depending on the length of the text, your application would look like one of the following screenshots:

In attempt to achieve this effect, I have seen several Android developers try to set the height of the view inside the scroll view to fill_parent. Doing so does not work and leads to the following result:

To understand this result, you must remember that android:layout_height=”fill_parent” means “set the height to the height of the parent.” This is obviously not what you want when using a ScrollView. After all, the ScrollView would become useless if its content was always as tall as itself. To work around this, you need to use the ScrollView attribute called android:fillViewport. 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.

The XML I wrote to create the correct version of this example can be found below. In line 32, I’ve set the android:layout_weight of the TextView to 1.0. By doing so I am forcing the text to use the available empty space when it is shorter than the ScrollView. This can only work when android:fillViewport=”true” is set on the scroll view.

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/scroller"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:fillViewport="true" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical">        <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:paddingLeft="6dip"            android:paddingRight="6dip"            android:paddingTop="6dip"            android:textAppearance="?android:attr/textAppearanceMedium"            android:text="Welcome to My Application" />

        <View            android:layout_width="fill_parent"            android:layout_height="1dip"            android:background="#ff106510"            android:layout_marginLeft="6dip"            android:layout_marginRight="6dip"            android:layout_marginTop="6dip"            android:layout_marginBottom="12dip" />

        <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_weight="1.0"

            android:paddingLeft="6dip"            android:paddingRight="6dip"            android:paddingBottom="6dip"

            android:text="@string/hello" />

        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="wrap_content"

            android:background="@android:drawable/bottom_bar"            android:gravity="center_vertical">            <Button                android:layout_width="0dip"                android:layout_weight="1.0"                android:layout_height="wrap_content" 

                android:text="Accept" />            <Button                android:layout_width="0dip"                android:layout_weight="1.0"                android:layout_height="wrap_content" 

                android:text="Refuse" />        </LinearLayout>    </LinearLayout></ScrollView>

Last but not least, I realized while writing this that the documentation of ScrollView does not mention the fillViewport XML attribute, even though it shows the equivalent Java API, setFillViewport(). This is a stupid mistake on my part that I will fix next week for a future release of Android. In the meantime, please accept my apologies :) just fixed.

转载于:https://www.cnblogs.com/RayLee/archive/2010/09/07/1820916.html

ScrollView’s handy trick相关推荐

  1. ScrollView’s handy trick--android:fillViewport=quot;truequot;

    ScrollView is one of Android's most commonly used widget and is also one of the easiest to use. When ...

  2. 构建一个react项目_您想要了解更多有关React的内容吗? 让我们构建一个游戏,然后玩。...

    构建一个react项目 by Samer Buna 通过Samer Buna 您想要了解更多有关React的内容吗? 让我们构建一个游戏,然后玩. (Do you want to learn more ...

  3. 使用NumPy优于Python列表的优势

    In this article, I will show a few neat tricks that come with NumPy, yet are must faster than vanill ...

  4. 自定义异常禁用异常堆栈_如何在Mac上禁用或自定义自动更正

    自定义异常禁用异常堆栈 Sometimes, autocorrect gets it wrong, replacing a word you meant to type with something ...

  5. dropbox_来自提示框:望远镜激光瞄准器,Dropbox桌面和Kindle剪辑转换

    dropbox Once a week we round up some great reader tips and share them with everyone; this week we're ...

  6. 在Raspberry Pi上设置Docker的简单方法

    by Ryan Gordon 通过瑞安·戈登(Ryan Gordon) 在Raspberry Pi上设置Docker的简单方法 (The easy way to set up Docker on a ...

  7. UEFI boot: how does that actually work, then?

    本文转载于: https://www.happyassassin.net/2014/01/25/uefi-boot-how-does-that-actually-work-then/ 本文翻译于:ht ...

  8. 如何将鼠标和键盘连接到PlayStation 4

    Believe it or not, Sony's PlayStation 4 works with a mouse and keyboard. This makes it more convenie ...

  9. ubuntu桌面幻灯片_如何在Ubuntu中创建墙纸幻灯片

    ubuntu桌面幻灯片 Just like Windows 7 and OS X, Ubuntu has the ability to create a slideshow wallpaper tha ...

最新文章

  1. javascript回调函数笔记
  2. Widgets 整理
  3. 【FPGA】单端口RAM的设计(同步读、同步写)
  4. 3、kubernetes应用快速入门190625
  5. Eclipse中使用git前账号密码等的配置问题
  6. fixed 语句(C# 参考)
  7. 操作系统 —— 文件管理
  8. 管家婆打印自定义编辑_打印相关,人手一份!
  9. 【蓝桥杯单片机】Led+蜂鸣器+继电器
  10. Jenkins+GitHub+Xcode+fir搭了一个持续集成环境
  11. 数据平台之企业营销管理与分析平台建设
  12. 4 安卓安装路径_安卓逆向——APK安装流程
  13. 帆软超级链接使用(根据内容跳转不同页面、超级链接使用js并传参、超级链接参数传递)
  14. iOS8以后,跳转到设置页面
  15. 在vue中实现在线代码编辑器(lua) - ace/codemirror/monaco-editor
  16. 控制工程实践(8)——PID四种类型控制器(之比例-积分控制器PI)
  17. 微信指纹支付设置java_微信指纹支付怎么设置?微信指纹支付设置方法介绍
  18. word转chm文件
  19. deleteRow() 连续删除多行
  20. Chrome安装程序遇到错误 0xe0000008解决办法

热门文章

  1. linux时间配置文件,linux系统下的时间配置综述
  2. 算法分析 运动员循环赛_「98跑」大众跑者的训练比专业运动员更累!
  3. C++接收字符串数组_PHP常用字符串函数(1),PHP面试重点
  4. MySQL的用户和授权
  5. 树莓派wiringPi库详解
  6. oracle 意外事件,Oracle数据库REDO损坏ora-00333修复手札
  7. mybitys 动态sql 注释_mybatis注解动态sql详解
  8. 滑动窗口 - 替换后的最长重复字符
  9. 动态规划 - 买卖股票的最佳时机 III
  10. LeetCode每日一题 143. 重排链表