安卓开发手记—ListView下面跟着Button
来源:互联网 发布:用友软件建账步骤 编辑:程序博客网 时间:2024/05/22 11:53
需求:一、ListView下面跟着一个Button,由于ListView的长度可能会超出屏幕高度,需要使用ScrollView-LinearLayout作为父容器。
二、使用ScrollView嵌套ListView,结果是,ListView的高度只显示一行。
三、如何让ListView在ScrollView中展开显示?两步:
1、在ListView使用setAdapter后设置为ListView设置高度(根据子容器高度);
- public void setListViewHeightBasedOnChildren(ListView listView) {
- // 获取ListView对应的Adapter
- ListAdapter listAdapter = listView.getAdapter();
- if (listAdapter == null) {
- return;
- }
- int totalHeight = 0;
- for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
- // listAdapter.getCount()返回数据项的数目
- View listItem = listAdapter.getView(i, null, listView);
- // 计算子项View 的宽高
- listItem.measure(0, 0);
- // 统计所有子项的总高度
- totalHeight += listItem.getMeasuredHeight();
- }
- ViewGroup.LayoutParams params = listView.getLayoutParams();
- params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
- // listView.getDividerHeight()获取子项间分隔符占用的高度
- // params.height最后得到整个ListView完整显示需要的高度
- listView.setLayoutParams(params);
- }
2、ListView的每个Item必须是LinearLayout,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常。故此需要给Item嵌套一个LinearLayout的根布局。
以上是自己开发中遇到的问题,根据百度结果总结,可用。
引用出处:http://www.jb51.net/article/38508.htm
http://www.cnblogs.com/zhwl/p/3333585.html
0 0
- 安卓开发手记—ListView下面跟着Button
- listview 下面添加button方法
- 安卓开发之Button
- 安卓开发之Button
- listview请求数据源,会加载很多数据,需求就是在listview下面有一个button点击事件,当数据完成一个下面紧接着跟着button点击事件,进行点击跳转
- 安卓开发手记 intent 返回结果
- 安卓开发——指定Button的样式
- 安卓开发之ListView
- 安卓开发-button事件监听
- 安卓手记1
- Androidnote-安卓手记
- 安卓开发——隐藏ListView的滚动条
- 安卓开发13:ListView详细解读
- 安卓开发-ListView的使用
- 安卓开发去掉listview点击效果
- 安卓开发:ListView简单入门
- 安卓开发之ListView优化方案
- 安卓开发学习心得-------ListView基础
- iOS键盘设置透明色
- easyui 客户端分页
- ifconfig wlan0 promisc 与 iw wlan0 set type monitor 区别
- 通过友盟分享内容到新浪微博错误解决方法
- KSImageNamed-Xcode插件在xcode 6.4/6.3或其余版本中不能使用解决方案
- 安卓开发手记—ListView下面跟着Button
- Linux启动过程详解
- ubuntu 安装pip
- python 一个比较好的讲解beanstalkc模块的网站
- 据技术Hadoop入门理论系列之一----hadoop生态圈介绍
- android调用相应的应用市场进行评价功能开发
- Android中ViewPager配合Fragment使用避免重复加载
- hdu1231最大连续子序列(动态规划&最大连续子序列)
- MySqlConnection.get_IsPasswordExpired()异常未将对象引用设置到对象实例