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

Android中使用ListView实现自适应表格的更多相关文章

  1. Android中使用ListView绘制自定义表格(2)

    上回再写了<Android中使用ListView绘制自定义表格>后,很多人留言代码不全和没有数据样例.但因为项目原因,没法把源码全部贴上来.近两天,抽空简化了一下,做了一个例子. 效果图如 ...

  2. 【转】整理一下Android中的ListView

    原文网址:http://sunbofu.blog.51cto.com/6431507/1280441 Android中的listview目测是一个使用频率很高的组件,所以今天来总结一下listview ...

  3. Android中实现ListView圆角效果[转]

    本文演示如何Android中实现ListView圆角效果. 无论是网站,还是APP,人们都爱看一些新颖的视图效果.直角看多了,就想看看圆角,这几年刮起了一阵阵的圆角设计风:CSS新标准纳入圆角元素,特 ...

  4. Android中的ListView属性使用总结

    Android中使用ListView控件比较常见,如果能知道常用的一些属性使用,肯定会少很多坑. 1.ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景 ...

  5. android中设置ListView的选中的Item的背景颜色

    ListView中没有默认的选择颜色,只有选择Item后的焦点颜色,鼠标点击时Item有颜色,放开鼠标后颜色也就没有了,要实现放开鼠标后选择项的背景还是有颜色的. 1.配置main.xml <? ...

  6. Android中自定义ListView实现上拉加载更多和下拉刷新

    ListView是Android中一个功能强大而且很常用的控件,在很多App中都有ListView的下拉刷新数据和上拉加载更多这个功能.这里我就简单记录一下实现过程. 实现这个功能的方法不止一个,Gi ...

  7. Android 中关于ListView分割线的设置

    今天发现许多App上的listview的item之间的分割线都只显示了右边一部分,而左边的那一半则没有,第一反应则是给分割线设置一张背景图片就ok了: android:divider="@m ...

  8. Android中使用ListView实现分页刷新(线程休眠模拟)

    当要显示的数据过多时,为了更好的提升用户感知,在很多APP中都会使用分页刷新显示,比如浏览新闻,向下滑动到当前ListView的最后一条信息(item)时,会提示刷新加载,然后加载更新后的内容.此过程 ...

  9. Android中使用ListView实现分页刷新(线程休眠模拟)(滑动加载列表)

    当要显示的数据过多时,为了更好的提升用户感知,在很多APP中都会使用分页刷新显示,比如浏览新闻,向下滑动到当前ListView的最后一条信息(item)时,会提示刷新加载,然后加载更新后的内容.此过程 ...

随机推荐

  1. IT从业人员关注哪些问题

    技术人员关注的问题非常多,但常见的至少有以下6种.特此整理,抓住核心问题,解决它. 一个人的精力和时间往往非常有限,能把核心问题都解决到位就是成功. 1.职业规划 大家从读小学开始,就是在为职业规划过 ...

  2. PatentTips - Enhanced I/O Performance in a Multi-Processor System Via Interrupt Affinity Schemes

    BACKGROUND OF THE INVENTION This relates to Input/Output (I/O) performance in a host system having m ...

  3. Deep Learning for Nature Language Processing --- 第四讲(下)

    A note on matrix implementations 将J对softmax的权重W和每一个word vector进行求导: 尽量使用矩阵运算(向量化).不要使用for loop. 模型训练 ...

  4. sublime课程3 emmet插件中的常用符号有哪些

    sublime课程3 emmet插件中的常用符号有哪些 一.总结 一句话总结:emmet插件中的符号和css选择器里面哪些符号的意思很像. 1.+是干嘛的? 组合 2.{}是干嘛的? 标签里面的inn ...

  5. C语言数组初始化的问题

  6. Python数据分析环境和工具

    一.数据分析工作环境 Anaconda: Anaconda(水蟒)是一个科学计算软件发行版,集成了大量常用扩展包的环境,包含了 Python 解释器,conda 包管理工具,以及 NumPy.Pand ...

  7. [D3] Convert Input Data to Output Values with Linear Scales in D3

    Mapping abstract values to visual representations is what data visualization is all about, and that’ ...

  8. mysql 按日期分组

    select DATE_FORMAT(NOW(),'%Y%m%d') days,count(caseid) count from tc_case group by days; //date_forma ...

  9. 从程序员的角度分析微信小程序(编程语言:用到什么学什么)

    从程序员的角度分析微信小程序(编程语言:用到什么学什么) 一.总结 一句话总结:微信小程序原理就是用JS调用底层native组件,和React Native非常类似.(需要时,用到时再学) 1.选择语 ...

  10. Thinking in UML 学习笔记(二)——UML核心视图之用例图

    在UML中,需求模型又称为用例模型,它主要用于描述系统的功能性需求,即软件可以实现的功能,如登录.注册.入库.出库.查看库存报表.增加员工信息等.常规的用例建模一般包括两个组成部分:绘制用例图和编写用 ...