Android 自定义TextView控件,用来组成表格方便数据的展示。

首先看一下效果

样式不是很好看,需要用的可以自己优化一下。

实现方式很简单。

  1、自定义控件 MyTableTextView 继承 TextView 重写onDraw方法。在里面添加话边框的操作。

 package lyf.com.mytableview;

 import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView; /**
* lyf on 2016/06/27
* 自定义TextView
*/
public class MyTableTextView extends TextView { Paint paint = new Paint(); public MyTableTextView(Context context, AttributeSet attrs) {
super(context, attrs);
int color = Color.parseColor("#80b9f2");
// 为边框设置颜色
paint.setColor(color);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 画TextView的4个边
canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint);
canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint);
}
}

  2、主布局,什么也不用写。直接放一个LinearLayout就好。但因为表格的宽度和高度往往超过屏幕的宽高度,因此需要我们添加ScrollView 和HorizontalScrollView。为了让HorizontalScrollView填满整个ScrollView 需要在ScrollView设置属性 android:fillViewport="true"。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none"
> <HorizontalScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarAlwaysDrawHorizontalTrack="false"
android:scrollbars="none"> <LinearLayout
android:id="@+id/MyTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="9dp"
android:orientation="vertical"
>
</LinearLayout> </HorizontalScrollView>
</ScrollView>
</LinearLayout>

  3、接下来写表格显示的样式文件table.xml。该布局文件用来显示表格每一行的样式。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"> <lyf.com.mytableview.MyTableTextView
android:id="@+id/list_1_1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/list_1_2"
android:layout_alignTop="@+id/list_1_2"
android:gravity="center"
android:textColor="#000"
android:textSize="13sp" /> <lyf.com.mytableview.MyTableTextView
android:id="@+id/list_1_2"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/list_1_1"
android:gravity="center"
android:textColor="#000"
android:textSize="13sp" /> <lyf.com.mytableview.MyTableTextView
android:id="@+id/list_1_3"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/list_1_2"
android:layout_alignTop="@+id/list_1_2"
android:layout_toRightOf="@+id/list_1_2"
android:gravity="center"
android:textColor="#000"
android:textSize="13sp" /> <lyf.com.mytableview.MyTableTextView
android:id="@+id/list_1_4"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/list_1_3"
android:layout_alignTop="@+id/list_1_3"
android:layout_toRightOf="@+id/list_1_3"
android:gravity="center"
android:textColor="#000"
android:textSize="13sp" /> <lyf.com.mytableview.MyTableTextView
android:id="@+id/list_1_5"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/list_1_4"
android:layout_alignTop="@+id/list_1_4"
android:layout_toRightOf="@+id/list_1_4"
android:gravity="center"
android:textColor="#000"
android:textSize="13sp" /> <lyf.com.mytableview.MyTableTextView
android:id="@+id/list_1_6"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/list_1_5"
android:layout_alignTop="@+id/list_1_5"
android:layout_toRightOf="@+id/list_1_5"
android:gravity="center"
android:textColor="#000"
android:textSize="13sp" /> <lyf.com.mytableview.MyTableTextView
android:id="@+id/list_1_7"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/list_1_6"
android:layout_alignTop="@+id/list_1_6"
android:layout_toRightOf="@+id/list_1_6"
android:gravity="center"
android:textColor="#000"
android:textSize="13sp" />
</RelativeLayout>

  4、最后把数据放到我们的table.xml文件中,并显示到我们的主布局文件中。

package lyf.com.mytableview;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RelativeLayout; /**
* lyf on 2016/06/27
* 自定义表格显示
*/ public class MainActivity extends Activity { private LinearLayout mainLinerLayout;
private RelativeLayout relativeLayout;
private String[] name={"序号","考号","姓名","出生年月","语文","数学","英语"}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLinerLayout = (LinearLayout) this.findViewById(R.id.MyTable);
initData();
} //绑定数据
private void initData() {
//初始化标题
relativeLayout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.table, null);
MyTableTextView title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_1);
title.setText(name[0]);
title.setTextColor(Color.BLUE); title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_2);
title.setText(name[1]);
title.setTextColor(Color.BLUE);
title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_3);
title.setText(name[2]);
title.setTextColor(Color.BLUE);
title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_4);
title.setText(name[3]);
title.setTextColor(Color.BLUE);
title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_5);
title.setText(name[4]);
title.setTextColor(Color.BLUE);
title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_6);
title.setText(name[5]);
title.setTextColor(Color.BLUE);
title = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_7);
title.setText(name[6]);
title.setTextColor(Color.BLUE);
mainLinerLayout.addView(relativeLayout); //初始化内容
int number = 1;
for (int i=0;i<10;i++) {
relativeLayout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.table, null);
MyTableTextView txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_1);
txt.setText(String.valueOf(number)); txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_2);
txt.setText("320321**********35");
txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_3);
txt.setText("张三");
txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_4);
txt.setText("1992/04/21");
txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_5);
txt.setText("150");
txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_6);
txt.setText("200");
txt = (MyTableTextView) relativeLayout.findViewById(R.id.list_1_7);
txt.setText("120");
mainLinerLayout.addView(relativeLayout);
number++;
}
}
}

下载链接

Android 自定义表格显示数据的更多相关文章

  1. 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)

    1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...

  2. Android自定义照相机实现(拍照、保存到SD卡,利用Bundle在Acitivity交换数据)

    Android自定义照相机实现 近期小巫在学校有一个创新项目,也不是最近,是一个拖了很久的项目,之前一直没有去搞,最近因为要中期检查,搞得我跟小组成员一阵忙活,其实开发一款照相机软件并不太难,下面就是 ...

  3. 【朝花夕拾】Android自定义View篇之(八)多点触控(上)MotionEvent简介

    前言 在前面的文章中,介绍了不少触摸相关的知识,但都是基于单点触控的,即一次只用一根手指.但是在实际使用App中,常常是多根手指同时操作,这就需要用到多点触控相关的知识了.多点触控是在Android2 ...

  4. Android自定义View4——统计图View

    1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...

  5. (转)[原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

  6. Android 自定义View合集

    自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...

  7. Android 自定义ListView

    本文讲实现一个自定义列表的Android程序,程序将实现一个使用自定义的适配器(Adapter)绑定 数据,通过contextView.setTag绑定数据有按钮的ListView. 系统显示列表(L ...

  8. [原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

  9. Android 自定义ScrollView ListView 体验各种纵向滑动的需求

      分类: [android 进阶之路]2014-08-31 12:59 6190人阅读 评论(10) 收藏 举报 Android自定义ScrollView纵向拖动     转载请标明出处:http: ...

随机推荐

  1. {part1}DFN+LOW(tarjan)割点

    什么是jarjan? 1)求割点 定义:在无向连通图中,如果去掉一个点/边,剩下的点之间不连通,那么这个点/边就被称为割点/边(或割顶/桥). 意义:由于割点和割边涉及到图的连通性,所以快速地求出割点 ...

  2. zip伪加密文件分析(进阶版)

    作者近日偶然获得一misc题,本来以为手到擒来,毕竟这是个大家都讨论烂了的题,详情访问链接http://blog.csdn.net/ETF6996/article/details/51946250.既 ...

  3. DataTable 批量插入SqlServer数据库 使用:SqlBulkCopy

    简单使用: private void UpdateTitle(DataTable dt) { ) { using (SqlBulkCopy sbc = new SqlBulkCopy(SqlHelpe ...

  4. Python-Mac 安装 PyQt4

    环境: 系统: OS X 10.11.4 Python: 2.7.10 1.安装 Qt brew install qt 测试安装结果,需要正确找到 qmake 的路径 qmake 2.安装 SIP 下 ...

  5. Oracle 11g r2 安装

    Help Center:http://docs.oracle.com/cd/E11882_01/install.112/e24326/toc.htm#i1011296 前提:linux需要安装图形化介 ...

  6. geohash基本原理

    geohash基本原理是将地球理解为一个二维平面,将平面递归分解成更小的子块,每个子块在一定经纬度范围内拥有相同的编码,这种方式简单粗暴,可以满足对小规模的数据进行经纬度的检索 目录: 经纬度常识 认 ...

  7. python print 进度条的例子

    def progress(width, percent): print "%s %d%%\r" % (('%%-%ds' % width) % (width * percent / ...

  8. zabbix3.0安装【server】

    关于zabbix的介绍,就不多说了,功能强大,架构前卫,自己直接去官网研究 在这里,还是秉承研究一个应用或者技术,都要自己动手安装部署,实战操作,才能深入掌握,基于这个精神,zabbix从安装部署开始 ...

  9. tomcat与oracle关于8080端口的冲突

    http://blog.csdn.net/baidu_24256693/article/details/44863935 由于电脑里同时安装了tomcat与oracle,所以8080的端口是冲突的. ...

  10. VES Hand Book Contents

    3...ABOUT THE VES4...Foreword 6...Chapter 1......Introduction6......Visual Effects and Special Effec ...