Android中使用ListView实现自适应表格
GridView比ListView更容易实现自适应的表格,但是GridView每个格单元的大小固定,而ListView实现的表格可以自定义每个格单元的大小,但因此实现自适应表格也会复杂些(格单元大小不一)。另外,GridView实现的表格可以定位在具体某个格单元,而ListView实现的表格则只能定位在表格行。因此还是那句老话:根据具体的使用环境而选择GridView 或者 ListView实现表格。
先贴出本文程序运行的效果图:
本文实现的ListView表格,可以每个格单元大小不一,文本(TextView)或图片(ImageView)做格单元的数据,不需要预先定义XML实现样式(自适应的根本目标)。由于ListView置于HorizontalScrollView中,因此对于列比较多/列数据比较长的数据表也能很好地适应其宽度。
main.xml源码如下:
view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<HorizontalScrollView android:id="@+id/HorizontalScrollView01"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<ListView android:id="@+id/ListView01" android:layout_height="wrap_content"
android:layout_width="wrap_content"></ListView>
</HorizontalScrollView>
</LinearLayout>
主类testMyListView.java的源码如下:
view plaincopy to clipboardprint?
package com.testMyListView;
import java.util.ArrayList;
import com.testMyListView.TableAdapter.TableCell;
import com.testMyListView.TableAdapter.TableRow;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;
/**
* @author hellogv
*/
public class testMyListView extends Activity {
/** Called when the activity is first created. */
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("ListView自适应实现表格---hellogv");
lv = (ListView) this.findViewById(R.id.ListView01);
ArrayList<TableRow> table = new ArrayList<TableRow>();
TableCell[] titles = new TableCell[5];// 每行5个单元
int width = this.getWindowManager().getDefaultDisplay().getWidth()/titles.length;
// 定义标题
for (int i = 0; i < titles.length; i++) {
titles[i] = new TableCell("标题" + String.valueOf(i),
width + 8 * i,
LayoutParams.FILL_PARENT,
TableCell.STRING);
}
table.add(new TableRow(titles));
// 每行的数据
TableCell[] cells = new TableCell[5];// 每行5个单元
for (int i = 0; i < cells.length - 1; i++) {
cells[i] = new TableCell("No." + String.valueOf(i),
titles[i].width,
LayoutParams.FILL_PARENT,
TableCell.STRING);
}
cells[cells.length - 1] = new TableCell(R.drawable.icon,
titles[cells.length - 1].width,
LayoutParams.WRAP_CONTENT,
&nb
最新文章
- 初学后台框架总结篇二——快速了解CI框架
- Android --资料集合
- 第一次写python爬虫
- leetcode 233 Number of Digit One
- 51nod 第K大区间2(二分+树状数组)
- Java发送post请求
- sqlserver2005唯一性约束
- iptables 配置需要保存
- 如何通过js使搜索关键词高亮
- TCP和HTTP
- 封装使用ViewHolder模式的Adapter组件,及用其快速实现聊天界面
- 201521123030《Java程序设计》第6周学习总结
- 配置SharePoint环境加域提示网络名不可用[已解决]
- Solve fatal error: helper_math.h: No such file or directory
- pwnable.kr-col-witeup
- String笔记
- 深入学习主成分分析(PCA)算法原理(Python实现)
- 数据库 -- pymysql
- Transaction rolled back because it has been marked as rollback-only 原因 和解决方案
- 通过MFC设计一个简单的计价程序
热门文章
- 【u247】生物进化
- 【39.66%】【codeforces 740C】Alyona and mex
- AE内置Command控件使用
- hadoop集群中的日志文件 分类: A1_HADOOP 2015-02-28 20:37 680人阅读 评论(0) 收藏
- Opencv距离变换distanceTransform应用——细化字符轮廓&&查找物体质心
- 【32.22%】【codeforces 602B】Approximating a Constant Range
- [Angular Unit Testing] Testing Services with dependencies
- [Angular Unit Testing] Shallow Pipe Testing
- [tmux] Customize tmux with tmux.conf
- 关闭 You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法