安卓中級教程(4):ScrollView與ListView之間的高度問題
在scrollView中加插ListView是一個大難題。其中一個難題是Listview的高度難以計算,輸出效果往往強差人意,就讓我們看看當中的問題 。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFE1FF"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="vertical"
android:fadingEdgeLength="5dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
大家可以看到ListView中的文字行列只能顯示一行,界面什差,如何解決高度問題,讓我們重新設計一下。
public class MainActivity extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
String[] adapterData = new String[] { "Afghanistan", "Albania",… … "Bosnia"};
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData));
setListViewHeightBasedOnChildren(listView);
}
public void setListViewHeightBasedOnChildren(ListView listView) {
// 獲取ListView對應的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
} int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
// listAdapter.getCount()返回數據項的數目
View listItem = listAdapter.getView(i, null, listView);
// 計算子項View 的寛高
listItem.measure(0, 0);
// 統計所有子項的總高度
totalHeight += listItem.getMeasuredHeight();
} ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// listView.getDividerHeight()獲取子項間分隔符占用的高度
// params.height最后得到整个ListView完整顯示需要的高度
listView.setLayoutParams(params); }}
安卓中級教程(4):ScrollView與ListView之間的高度問題的更多相关文章
- 安卓中級教程(3):ScrollView
以上是scrollview的圖例,可見srollview是一種滑動功能的控件,亦是非常常見的控件. 一般寫法如下: package com.mycompany.viewscroller; import ...
- 安卓中級教程(5):ScrollView與refreshable之間的設置
設置向下拉動更新. package com.mycompany.Scroll_test; import android.app.*; import android.os.*; import andro ...
- 安卓中級教程(10):@InjectView
package com.example.android.db01; import android.app.Activity; import android.content.ContentValues; ...
- 安卓中級教程(9):pathbutton中的animation.java研究(2)
src/geniuz/myPathbutton/composerLayout.java package geniuz.myPathbutton; import com.nineoldandroids. ...
- 安卓中級教程(8):pathbutton中的animation.java研究(1)
src/geniuz/myPathbutton/myAnimations.java package geniuz.myPathbutton; import java.util.ArrayList; i ...
- 安卓中級教程(6):annotation的基本用法
package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...
- 安卓中級教程(1):@InjectView
package com.mycompany.hungry; import android.annotation.SuppressLint; import android.app.Activity; i ...
- 安卓中級教程(11):深入研究餓了麼的各個java檔運作關係(1)
package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...
- 安卓中級教程(7):annotation中的 public @interface的用法
package com.example.ele_me.util; import java.lang.annotation.Retention; import java.lang.annotation. ...
随机推荐
- CPU的内部架构和工作原理 (转,相当不错)
http://blog.chinaunix.net/uid-23069658-id-3563960.html 一直以来,总以为CPU内部真是如当年学习<计算机组成原理>时书上所介绍的那样, ...
- hadoop 2.6 centos 7.1 下的一些操作
开启hdfs: start-dfs.sh 开启yarn: start-yarn.sh 查看hdfs:http://ip:50070/ 查看RM:http://ip:8088/
- CF720F
题目描述 给定1≤n≤100000,1≤k≤n(n-1)/2,和〈a_1, ... ,a_n〉,其中-50000≤a_i≤50000, 要求选出k个不同的子段,使得每个元素都被这些子段覆盖且这k个子段 ...
- POI导出excel日期格式
参考帖子: [1]http://www.ithao123.cn/content-2028409.html [2]http://javacrazyer.iteye.com/blog/894850 再读本 ...
- java基本算法之快速排序
快速排序:是找出一个元素(理论上可以随便找一个)作为基准(pivot),然后对数组进行分区操作,使基准左边元素的值都不大于基准值,基准右边的元素值 都不小于基准值,如此作为基准的元素调整到排序后的正确 ...
- 安装oracle常见问题和解决方案
./runInstaller: line 95: [: too many arguments ./runInstaller: line 99: /home/oracle/oracle/database ...
- SQL入门语句之SELECT和WHERE
一.SQL入门语句之SELECT SELECT语句用于从数据库表中获取数据,结果表的形式返回数据.这些结果表也被称为结果集 1.从数据库表中取部分字段 select 字段A,字段B from tabl ...
- iOS之ToolBar定制
ToorBar的定制 在诸如社区类的app里面,很多都涉及到用户发布消息,如现今最流行的新浪微博,每条信息底部都会有个工具条,正如下图所示,有转发.评论和点赞三个按钮的工具条. 结构 1.作为一个独立 ...
- 第二章 编写与设置Servlet
2.1 第一个Servlet package cc.openhome; import javax.servlet.ServletException; import javax.servlet.http ...
- mysql插入速度
请参考 http://www.3lian.com/edu/2013/07-15/80916.html 分表查询 select * from ( select * from user0 un ...