第一种:按照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>

第三种

public static void setListViewHeightBasedOnChildren(ListView listView, int attHeight) {  
        ListAdapter listAdapter = listView.getAdapter();   
        if (listAdapter == null) {  
            // pre-condition  
            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)) + attHeight;  
        listView.setLayoutParams(params);  
    }  
 
}

最新文章

  1. C#中的new修饰符
  2. solr与.net系列课程(四)solr查询参数的讲解与.net如何获取solr数据
  3. html 实体转换为字符:转换 UEditor 编辑器 ( 在 ThinkPHP 3.2.2 中 ) 保存的数据
  4. word2007里插入分节符
  5. 看人家如何拿到腾讯阿里的offer
  6. C# get set方法
  7. OpenCV学习 2:播放AVI视频
  8. 【转载】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型
  9. JQuery -&amp;gt; 超级简单的下拉菜单
  10. JAVA提高五:注解Annotation
  11. 分布式版本库——Windows下Git的环境部署以及在GitHub上开源自己的项目
  12. DataPipeline CTO陈肃:从ETL到ELT,AI时代数据集成的问题与解决方案
  13. pc端布局的一点思考
  14. redis 配置文件解释 以及集群部署
  15. xslfo和fop使用中的一些问题
  16. FTP 150 Opening BINARY mode data connection for MLSD 读取目录列表失败
  17. EntityFramework Core
  18. 【SE】Week2 : 个人博客作业
  19. 垂直居中Demo
  20. spring cloud 消费者

热门文章

  1. 《A Tour of PostgreSQL Internals》学习笔记——查询处理分析
  2. Redis之hash数据结构实现
  3. 下载百度网盘破解 获得 所下载视频URL 粘贴到thunder
  4. python 列表求和
  5. H5 canvas建造敌人坦克
  6. Spring 及 SpringMVC的web.xml配置详解
  7. hdu2897找规律
  8. 微信小程序引入md5.js
  9. PHP:第五章——字符串输出函数
  10. tensorflow :ckpt模型转换为pytorch : hdf5模型