最近做项目需要用到表格,数据是动态指定的,本来用GridView是很方便的,可是老大不同意用这么重量级的控件,想办法吧。

做表格很容易想到用TableLayout,那就自定义一个来搞。

一、表格最蛋疼的就是那根线,网上有个很好的方法,大概思路是这样的:

1、给表格设置一个背景色(线的颜色)

2、给表格设置一个内边距(线的宽度的一半)

3、设置每一项内边距(线的宽度的一半)

3、给项的内容设置一个背景色(颜色不同于线即可)

显示效果大概是这样的:

二、数据的动态加载使用Adapter类,便与布局加载

自定义TableLayout加载的主体方法:

public void setAdapter(BaseAdapter baseAdapter, int column) {
    if (baseAdapter == null || baseAdapter.getCount() == 0) {
        return;
    }
    this.mAdapter = baseAdapter;
    this.column = column;
    drawLayout();
}
private void drawLayout() {
    removeAllViews();
    int realcount = mAdapter.getCount();
    int count = 0;
    if (realcount < column) {
        count = column;
    } else if (realcount % column != 0) {
        count = realcount + column - (realcount % column);
    } else {
        count = realcount;
    }
    TableRow tableRow = null;//每一行的TableRow
    for (int i = 0; i < count; i++) {
        final int index = i;
        View view = null;
        if (index >= realcount) {
        view = mAdapter.getView((realcount - 1), null, null);
        view.setVisibility(View.INVISIBLE);
        } else {
        view = mAdapter.getView(index, null, null);
        }
        if (index % column == 0) {// 整行
        tableRow = new TableRow(mContext);
        }
        if (tableRow != null) {//添加每一个Item
        tableRow.addView(view);
        }
        if (index % column == 0) {// 整行
        addView(tableRow, new TableLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        }
    }
}

三、使用和ListView的方式基本一样

public class MainActivity extends Activity {

    private List<Map<String, Object>> dataList;

    private TableBorderLayout layTable;

    private String[] datas = new String[] { "疯狂", "个性", "张扬", "抖擞", "加油", "奋斗",
        "努力", "精神" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      initViews();
      loadDatas();
    }

    private void initViews() {
      layTable = (TableBorderLayout) findViewById(R.id.layTable);
    }

    private void loadDatas() {
      dataList = new ArrayList<Map<String, Object>>();
      Map<String, Object> item = null;
      for (int i = 0; i < datas.length; i++) {
          item = new HashMap<String, Object>();
          item.put("Title", datas[i]);
          dataList.add(item);
      }
      layTable.setAdapter(new MyAdapter(this, dataList));
    }

}

感兴趣的可以去这个地址下载:https://github.com/PearLemon/TableBorderLayout.git

Android带边框表格的实现的更多相关文章

  1. Android ------------------ 带边框的圆角矩形

    <?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http:/ ...

  2. android 带边框的圆角按钮

    新建buttonstyle.xml 代码如下 <?xml version="1.0" encoding="UTF-8"?> <layer-li ...

  3. Android自定义控件 -- 带边框的TextView

    使用xml实现边框 原来使用带边框的TextView时一般都是用XML定义来完成,在drawable目录中定义如下所示的xml文件: <?xml version="1.0" ...

  4. Android GridView去除自带边框点击效果、去除右侧滚动条、禁止上下滑动

    一.去除自带边框点击效果: <com.example.gridview.MyGridView android:id="@+id/grid_upload_pictures" a ...

  5. 自定义带边框TextView--边框粗细不一的问题

    自定义带边框TextView 给textview加边框 最low的做法.textview外层套一层布局,然后给布局加边框样式(这么弱的做法,不能这么干) 自定义控件 canvas.drawLines ...

  6. css挤带边框的三角

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  7. UIImage类扩展返回一个带边框的圆形图片

    /** * 将image转换为圆型带边框的图片(最好写一个UIImage的类扩展) * * @param name 图片的名字 * @param borderWidth 外层边框的宽度 * @para ...

  8. Android -- 带你从源码角度领悟Dagger2入门到放弃

    1,以前的博客也写了两篇关于Dagger2,但是感觉自己使用的时候还是云里雾里的,更不谈各位来看博客的同学了,所以今天打算和大家再一次的入坑试试,最后一次了,保证最后一次了. 2,接入项目 在项目的G ...

  9. Android -- 带你从源码角度领悟Dagger2入门到放弃(二)

    1,接着我们上一篇继续介绍,在上一篇我们介绍了简单的@Inject和@Component的结合使用,现在我们继续以老师和学生的例子,我们知道学生上课的时候都会有书籍来辅助听课,先来看看我们之前的Stu ...

随机推荐

  1. LeetCode 36 Valid Sudoku

    Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...

  2. iOS url中文编码

    有两种方法: 一,使用NSString的方法: NSString* string2 = [string1 stringByAddingPercentEscapesUsingEncoding:NSUTF ...

  3. qt 定时器

    重写方法 virtual void timerEvent(QTimerEvent *event); 启动定时器 timerId = startTimer(500);

  4. 解决Eclipse Failed to write core dump. Minidumps are not enabled by default on client versions

    -XX:+CreateMinidumpOnCrash 如果还有问题,就只能看日志了 T_T

  5. 攻城狮在路上(陆)-- hadoop分布式环境搭建(HA模式)

    一.环境说明: 操作系统:Centos6.5 Linux node1 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 ...

  6. docker学习

    一.基本概念1.Docker镜像 Docker镜像(Image)就是一个只读的模板. 例如:一个镜像可以包含一个完整的ubuntu操作系统环境,里面仅安装了Apache或用户需要的其它应用程序. 镜像 ...

  7. ExtJS 中类的继承

    ExtJS 允许对现有的类进行扩展,其扩展可以通过继承来实现.接下来我们就对刚刚使用ExtJS定义的Person类进行继承,定义一个Developer类,它继承自Person,同时还拥有Coding方 ...

  8. gdb的可视化工具安装

    红帽推出的insight https://www.sourceware.org/insight/index.php http://wiki.ubuntu.org.cn/Insight%E7%9A%84 ...

  9. LinkedList源码阅读笔记(基于JDK1.8)

    LinkedList是List接口的一个有序链表实现,存储节点是内部类Node,Node中有两个属性prev和next,负责连接前后两个元素.由于不是使用数组进行存储,所以查询需要遍历链表一半的元素( ...

  10. 设计模式 之 原型模式(ProtoType)

    什么时原型模式   或   原型模式的定义: 用原型实例来指定创建对象的种类,并通过拷贝这些原型创建新的对象. 原型模式的特点: 1),它是面向接口编程, 2),原型模式的新对象是对原型实例的一个克隆 ...