Android ListView根据项数的大小自动改变高度
第一种:按照listview的项数确定高度
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() – 1));
((MarginLayoutParams)params).setMargins(10, 10, 10, 10);
listView.setLayoutParams(params);
第二种:直接使用当前界面尺寸,稍加调整
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = getWindowManager().getDefaultDisplay().getHeight() – 30;
// Toast.makeText(this, params.height+"", 3000).show();
listView.setLayoutParams(params);
XML布局写法,请注意这里需要一个内部LinerLayout
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdge = "none"
android:background="#FFF4F4F4"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="#fff4f4f4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/moreItemsListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#FFF4F4F4"
android:dividerHeight="0.0dip"
android:fadingEdge="none"
/>
</LinearLayout>
</ScrollView>
第三种
最新文章
- C#中的new修饰符
- solr与.net系列课程(四)solr查询参数的讲解与.net如何获取solr数据
- html 实体转换为字符:转换 UEditor 编辑器 ( 在 ThinkPHP 3.2.2 中 ) 保存的数据
- word2007里插入分节符
- 看人家如何拿到腾讯阿里的offer
- C# get set方法
- OpenCV学习 2:播放AVI视频
- 【转载】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型
- JQuery -&;gt; 超级简单的下拉菜单
- JAVA提高五:注解Annotation
- 分布式版本库——Windows下Git的环境部署以及在GitHub上开源自己的项目
- DataPipeline CTO陈肃:从ETL到ELT,AI时代数据集成的问题与解决方案
- pc端布局的一点思考
- redis 配置文件解释 以及集群部署
- xslfo和fop使用中的一些问题
- FTP 150 Opening BINARY mode data connection for MLSD 读取目录列表失败
- EntityFramework Core
- 【SE】Week2 : 个人博客作业
- 垂直居中Demo
- spring cloud 消费者