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

Android ListView根据项数的大小自动改变高度的更多相关文章

  1. Android ListView item项 显示动画

    (1)使用LayoutAnimation 所谓的布局动画,其实就是为ViewGroup添加显示动画效果,主要用过LayoutAnimationController来控制实现.LayoutAnimati ...

  2. android ListView 与GridView 学习总结(五)

    ListView的使用总结 基本使用:   步骤:在布局文件中定义一个ListView控件-在活动中获得ListView的实例-获得适配器adapter的实例并且传入三个参数-把适配器对象传递给lis ...

  3. 实现Android ListView 自动加载更多内容

    研究了几个小时终于实现了Android ListView 自动加载的效果. 说说我是怎样实现的.分享给大家. 1.给ListView增加一个FooterView,调用addFooterView(foo ...

  4. Android ListView 自动加载更多

    Android ListView下拉刷新 ListView是我们经常用来展示数据的一个控件,但是由于我们手机的性能和流量的问题,往往我们从服务器中取数据,不能一次性将数据取出来,比如一个新闻的手机AP ...

  5. 【腾讯Bugly干货分享】Android ListView与RecyclerView对比浅析--缓存机制

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/5811d3e3ab10c62013697408 作者:黄宁源 一,背景 Recy ...

  6. Android listview和gridview以及view的区别

    GridView 可以指定显示的条目的列数. listview一般显示的条目的列数都是一列 如果是列表(单列多行形式)的使用ListView,如果是多行多列网状形式的优先使用GridView andr ...

  7. android ListView 九大重要属性详细分析、

    android ListView 九大重要属性详细分析. 1.android ListView 一些重要属性详解,兄弟朋友可以参考一下. 首先是stackFromBottom属性,这只该属性之后你做好 ...

  8. android listview综合使用示例_结合数据库操作和listitem单击长按等事件处理

    本示例说明: 1.自定义listview条目样式,自定义listview显示列数的多少,灵活与数据库中字段绑定. 2.实现对DB的增删改查,并且操作后listview自动刷新. 3.响应用户操作点击事 ...

  9. 【转】android ListView 几个重要属性

    android ListView 几个重要属性 分类: Android2012-03-08 19:25 19324人阅读 评论(5) 收藏 举报 listviewandroid活动javalistnu ...

随机推荐

  1. 自动生成makefile

    原文  http://www.laruence.com/2009/11/18/1154.html 作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实 ...

  2. 七步精通Python机器学习--转载

    作者简介: Matthew Mayo    翻译:王鹏宇 开始.这是最容易令人丧失斗志的两个字.迈出第一步通常最艰难.当可以选择的方向太多时,就更让人两腿发软了. 从哪里开始? 本文旨在通过七个步骤, ...

  3. 【测试设计】性能测试工具选择:wrk?jmeter?locust?还是LR?

    原文链接:http://www.51testing.com/html/49/n-3721249.html 前言 当你想做性能测试的时候,你会选择什么样的测试工具呢?是会选择wrk?jmeter?loc ...

  4. java 获取本机的IP地址

    方法一:这种方式有一定的局限性,在Linux下的执行结果是:本机的IP = xxx/127.0.1.1 (其中xxx是你的计算机名) public void getLocalIPAddress() { ...

  5. call()与apply()用法

    call()和apply()的作用都是一样的——通过改变函数体内部 this 的指向,借用对象的方法的目的 还是举个栗子吧: function Cat(){ this.food = 'fish'; t ...

  6. 007PHP文件处理—— 判断文件与操作文件fopen fread fclose is_executable is_readable is_writeable

    <?php /** * 判断文件与操作文件fopen fread fclose * is_executable is_readable is_writeable */ //判断一个文件是不是一个 ...

  7. 042——VUE中组件之子组件使用$on与$emit事件触发父组件实现购物车功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. vue.js-读取/发送数据

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. bzoj1176

    题解: 和上一题差不多 就加上一个初始值 代码: #include<bits/stdc++.h> ; using namespace std; int n,m,cnt,s,sum[N],a ...

  10. ubuntu:NVIDIA设置性能模式,以降低CPU使用、温度

    NVIDIA设置性能模式,以降低CPU使用.温度 ubuntu安装完NVIDIA显卡驱动后 终端输入 nvidia-settings 选择OpenGL Settings->Image Setti ...