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 ScrollViewaccepts 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" ><LinearLayoutandroid: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" /><Viewandroid: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" /><LinearLayoutandroid: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.

From:http://www.curious-creature.com/2010/08/15/scrollviews-handy-trick/

ScrollView’s handy trick--android:fillViewport=quot;truequot;相关推荐

  1. ScrollView’s handy trick

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

  2. Android之ScrollView设置了高度(android:layout_height=“match_parent“)但里面的组件不能充满问题

    1 问题 写了ScrollView控件,设置了高度(android:layout_height="match_parent"),但是里面的组件不能填充它,感觉就像ScrollVie ...

  3. Android fillViewPort属性用法

    最近在开发项目中遇到一个问题,布局高度在某些国产酷派小屏幕手机上高度不够全部显示,于是使用了ScrollView嵌套LinearLayout,但问题又出现了,在大屏幕手机如三星note3手机上下面会留 ...

  4. Android ScrollView内部组件设置android:layout_height=fill_parent无效的解决办法

    问题:scrollview内部组件都设置了android:layout_height="fill_parent"却没有效果. 解决办法:设置scrollview的fillViewp ...

  5. android:fillviewport=true 不起作用,无法在android模拟器中滚动

    当方向处于横向时,是否可以使用android模拟器进行滚动?如果是的话,那么我一定是错过了什么..请帮助我...谢谢无法在android模拟器中滚动 android:layout_width=&quo ...

  6. android scrollview 滚动监听,Android开发之ScrollView的滑动监听

    我们需要监听ScroView的滑动情况,比如滑动了多少距离,是否滑到布局的顶部或者底部.可惜的是SDK并没有相应的方法,不过倒是提供了一个 protected void onScrollChanged ...

  7. android:catation=quot;90quot;,Android中的AlarmManager的使用.htm

    var protocol = window.location.protocol; document.write(' Android中的AlarmManager的使用 - wangxingwu_314的 ...

  8. EditText 输入类型 android:inputType=quot;参数类型quot;

    android:inputType="none"--输入普通字符android:inputType="text"--输入普通字符android:inputTyp ...

  9. android scrollview 底部控件,Android ScrollView和屏幕底部的按钮

    刚刚创建并测试了它.看起来像你想要的 android:layout_width="fill_parent" android:layout_height="fill_par ...

最新文章

  1. Appian宣布将Google AI 集成到RPA中
  2. MyBatis 批量更新,批量更新
  3. 没有内存条电脑能开机吗_开机键过时了!3种电脑开机方法,能让你睡懒觉的技术还不快来学...
  4. gvim安装及其配置
  5. php美顏滤镜,PHP GIF / PNG True Colorize滤镜,可保留亮度和Alpha
  6. [转载] 【Python】set() 集合操作与运算 元素输出顺序
  7. Ubuntu搜狗输入法ctrl+alt+b冲突解决
  8. 中国综合能源服务市场盈利模式分析与投资战略规划研究报告2022-2028年
  9. 台达伺服控制器接线图_伺服电机如何接线图
  10. Navicat 局域网连接数据库
  11. Lookup Transformation
  12. Typora 实用教程
  13. ue转换文件格式linux,关于windows与unix之间文件格式转换问题。UE编辑器中(CR/LF)问题...
  14. lending club 贷款逾期分析
  15. 机试评判系统评判提交程序后返回结果详细说明
  16. 言承旭签约尊尼事务所 变木村拓哉师弟攻日本
  17. 计算机在英语专业的应用论文,计算机英语双语应用分析论文
  18. 父类引用和子类引用之间 的关系
  19. fancybox iframe
  20. Anaconda - conda 常用命令

热门文章

  1. 微信 SQLite 数据库修复实践
  2. 【CV论文阅读】 Fast RCNN + SGD笔记
  3. FineReport——获取控件值和单元格值
  4. RFI远程文件包含的漏洞
  5. buildroot的使用简介【转】
  6. CSS基础---日记4
  7. 计算程序执行时间的函数
  8. Redis自定义动态字符串(sds)模块(二)
  9. easyUI的combobox设置隐藏和显示
  10. C#学习之用迭代器实现枚举器