android 中 listview 设置自动匹配高度
1.布局文件
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <ListView
android:id="@+id/lv0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0"
android:dividerHeight="5dp"/> <TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#0f0"
android:gravity="bottom"/> <ListView
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f0f"
android:dividerHeight="5dp"/> </LinearLayout>
</ScrollView>
2. java 代码
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView; import java.util.ArrayList;
import java.util.List; /**
* 设置ListView的自动高度
* 3种方式:
* 1.onWindowFocusChanged() 中设置
* 2.onResume() 中设置
* 3.onCreate() 中view.post()方法中设置
*/
public class ListViewAutoHeight extends Activity { private static final String TAG = "autoHeight";
private ListView lv0;
private ListView lv1; @Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// setListViewAutoHeight(lv0);
// setListViewAutoHeight(lv1);
Log.e(TAG, "onWindowFocusChanged ");
} @Override
protected void onResume() {
super.onResume();
// setListViewAutoHeight(lv0);
// setListViewAutoHeight(lv1);
Log.e(TAG, "onResume ");
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_listview_auto_height); lv0 = (ListView) findViewById(R.id.lv0);
List<String> lv0Datas = new ArrayList<>();
for (int i = 0; i < 10; i++) {
lv0Datas.add("item_0_" + i);
}
ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lv0Datas);
lv0.setAdapter(adapter0); lv1 = (ListView) findViewById(R.id.lv1);
List<String> lv1Datas = new ArrayList<>();
for (int i = 0; i < 20; i++) {
lv1Datas.add("item_1_" + i);
}
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lv1Datas);
lv1.setAdapter(adapter1); // 随便在哪个控件身上的post 方法中调用设置自动高度的方法即可
// post 是将该方法添加到Loop队列的末尾,如果直接调用是不起作用的
lv1.post(new Runnable() {
@Override
public void run() {
setListViewAutoHeight(lv0);
setListViewAutoHeight(lv1); Log.e(TAG, "post ");
}
});
} /** 计算高度 */
private void setListViewAutoHeight(ListView lv) {
if (lv == null || lv.getCount() <= 0) return;
int totalHeight = 0;
for (int i = 0; i < lv.getCount(); i++) {
View view = lv.getAdapter().getView(i, null, lv);
view.measure(0, 0);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = lv.getLayoutParams();
params.height = totalHeight + lv.getDividerHeight() * (lv.getCount() - 1) + lv.getPaddingTop() + lv.getPaddingBottom();
lv.setLayoutParams(params);
} }
android 中 listview 设置自动匹配高度的更多相关文章
- Android中Listview点击item不变颜色以及设置listselector 无效
Android中Listview点击item不变颜色以及设置listselector 无效 这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listsele ...
- Android中ListView控件的使用
Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...
- android中ListView点击和里边按钮点击不能同时生效问题解决
今天遇到一个问题:android中ListView点击和里边button点击不能同时生效问题解决. 原因是: listView 在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得 ...
- 在C#中使用正则表达式自动匹配并获取所需要的数据
转自:http://my.oschina.net/bv10000/blog/111736 正则表达式能根据设置匹配各种数据(比如:e-mail地址,电话号码,身份中号码等等).正则表达式功能强大,使用 ...
- Android中如何设置RadioButton在文字的右边,图标在左边
from:http://blog.csdn.net/sunnyfans/article/details/7901592?utm_source=tuicool&utm_medium=referr ...
- android中ListView控件&&onItemClick事件中获取listView传递的数据
http://blog.csdn.net/aben_2005/article/details/6592205 本文转载自:android中ListView控件&&onItemClick ...
- Android中ListView无法点击
Android中ListView无法点击 转自:http://xqjay19910131-yahoo-cn.iteye.com/blog/1319502 问题描述: ListView中Item加入 ...
- android中listview的一些样式设置
在Android中,ListView是最常用的一个控件,在做UI设计的时候,很多人希望能够改变一下它的背景,使他能够符合整体的UI设计,改变背景背很简单只需要准备一张图片然后指定属性 android: ...
- Android中ListView嵌套进ScrollView时高度很小的解决方案
package com.example.test.util; import android.view.View; import android.view.ViewGroup; import andro ...
随机推荐
- form表单类标签汇总
<form action="form_action.asp" method="get"> First name: <input type=&q ...
- .net平台下深拷贝和浅拷贝
在.net类库中,对象克隆广泛存在于各种类型的实现中,凡是实现了ICloneable接口的类型都具备克隆其对象实例的能力.所以本文讲述的深拷贝和浅拷贝也是在实现ICloneable接口的基础上进行的. ...
- linux input输入子系统应用分析
输入设备(如按键.键盘.触摸屏.鼠标等)是典型的字符设备,其一般的工作机理是底层在按键.触摸等动作发送时产生一个中断(或驱动通过timer定时查询),然后CPU通过SPI.I2 C或外部存储器总线读取 ...
- Windows Server 2008 R2 域控修改域用户密码复杂性
1.进入”本地安全策略”进行管理时,发现密码策略已经被锁定,无法更改: 2.在此情况下要改密码策略的过程如下, 进入组策略管理: 3.右键点击/编辑Default Domain Policy: 4.在 ...
- javascript设计模式学习之八_发布订阅(观察者)模式
一.发布订阅模式定义 jQuery中的callbacks,defered,promise本质上就是发布订阅模式的实现.ES6的promise内部实现未开源,不了解具体机制 发布订阅模式又叫做观察者模式 ...
- 使用xib封装一个自定义view的步骤
使用xib封装一个自定义view的步骤 1> 新建一个继承UIView的自定义view,假设类名叫做(MJAppView) 2> 新建一个MJAppView.xib文件来描述MJAppVi ...
- 使用JavaScript闭包,以工厂模式实现定时器对象
原始对象写法 一般工作中写Javascript代码,主要写全局函数,并组织函数之间的调用,确实比较低级, 于是想利用面向对象的思想应用到JS编码中. 在火狐浏览器开发者网站上,看到一个实例利用对象技术 ...
- Leetcode: Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- JS语法部分-数组
数组的长度是动态变化的,里面可以防止任意类型的元素 var a=new Array() 数组元素的复制:a[0]=123 a[2]=456 数组的取值:a[i] 数组的属性: a.le ...
- [转]java工程师成神之路
转载http://www.hollischuang.com/archives/489https://linux.cn/article-6739-1.html 一.基础篇 1.1 JVM 1.1.1. ...