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. QT5.5

    QT创建空项目时,会有报错“无法解析的外部符号”,方法是在 .pro文件中添加 以下,即可QT+=core gui widgets

  2. DSO Framer _ WinForm 使用

    根据自己对dsoframer控件的学习,想把dsoframer控件进行简单的包装为C#的usercontrol,大体需要作如下:(创建windows的usercontrol的步骤就不再说了...)我们 ...

  3. Swift3.0 功能二 (表情键盘与图文混排)

    随着iOS越来越多表情键盘以及图文混排的需求,本文运用Swift3.0系统的实现其功能以及封装调用方法,写的不好,如有错误望各位提出宝贵意见,多谢 项目源码地址: 相关知识点都有标识 项目源码地址 废 ...

  4. Swift入坑系列—集合类型

    数组(Arrays) 字典(Dictionaries) 数组(Arrays) 在OC里面,NSArray和NSMutableArray这两个类可以存储任意类型的对象,并且不提供所返回对象的任何特别信息 ...

  5. [CSS] Easily Reset Styles With a Single CSS value

    There are times where you need to reset a an element’s styles. Instead of overwriting it with even m ...

  6. 解决Eclipse中文乱码 分类: B1_JAVA 2014-06-25 11:23 336人阅读 评论(0) 收藏

    使用Eclipse编辑文件经常出现中文乱码或者文件中有中文不能保存的问题,Eclipse提供了灵活的设置文件编码格式的选项,我们可以通过设置编码 格式解决乱码问题.在Eclipse可以从几个层面设置编 ...

  7. HttpClient基础教程 分类: C_OHTERS 2014-05-18 23:23 2600人阅读 评论(0) 收藏

    1.HttpClient相关的重要资料 官方网站:http://hc.apache.org/ API:http://hc.apache.org/httpcomponents-client-4.3.x/ ...

  8. [Django] Get started with Django -- Install python and virtualenv

    Install python3 on MacOS: brew install python3 Come alone with python3, there are also some other to ...

  9. tip of Firefox extention foxyproxy

    tip of Firefox extention foxyproxy

  10. Android 开发者工具

    30多个Android 开发者工具 文中部分工具是收费的,但是绝大多数都是免费的. FlowUp 这是一个帮助你跟踪app整体性能的工具,深入分析关键的性能数据如FPS, 内存, CPU, 磁盘, 等 ...