第一种:按照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. S4 继承

    S3 系统是宽泛且灵活的,同类的 S3 对象也可能有不同的成员.但是,对于 S4 系统,就不会发生,也就是说,当我们创建一个属于某类的 S4 对象实例时,不能任意添加不在类表示中的字段.举个例子,在创 ...

  2. 遗传算法 Genetic Algorithm

    2017-12-17 19:12:10 一.Evolutionary Algorithm 进化算法,也被成为是演化算法(evolutionary algorithms,简称EAs),它不是一个具体的算 ...

  3. php多站点配置以及Forbidden You don't have permission to access / on this server问题解决

    php多站点配置以及Forbidden You don't have permission to access / on this server问题解决 一.总结 一句话总结:我的问题是php的版本问 ...

  4. 导出csv文件,导出axlsx文件。gem 'Axlsx-Rails' (470🌟);导入csv文件。

    汇出 CSV 档案 需求:后台可以汇出报名资料 有时候后台功能做再多,也不如 Microsoft Excel 或 Apple Numbers 试算表软件提供的分析功能,这时候如果有汇出功能,就可以很方 ...

  5. ✅问题:Rails.ajax自定义请求

    chatroom.coffee中的js代码: document.addEventListener 'turbolinks:load', -> document.getElementById(&q ...

  6. git-it 教程,一些git知识点。/ 如何解决merge conflict/ 如何使用Github Pages./Git术语表

    一个git使用教程 https://:.com/jlord/git-it-electron#what-to-install 一个在线Github的功能教学:https://lab.github.com ...

  7. [转载]SQL语句练习

    .查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号,成绩) - 临时表 根据[学号]连接两个临时表: 学号 ...

  8. 识别User Agent屏蔽一些Web爬虫防采集

    识别User Agent屏蔽一些Web爬虫防采集 from:https://jamesqi.com/%E5%8D%9A%E5%AE%A2/%E8%AF%86%E5%88%ABUser_Agent%E5 ...

  9. 360插件化Replugin爬坑之路

    前言 继上次爬完了热修复的坑位,中途爬了各种各样的坑.今天我们来说说插件化Replugin的坑位.Replugin刚出的时候我就看过了.第一次看的时候可能心态不好.没看懂= =第二次重头在看,发现蛮简 ...

  10. 史上最全的maven的pom.xml文件详解(转载)

    此文出处:史上最全的maven的pom.xml文件详解——阿豪聊干货 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...