Android 自定义表格显示数据
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 自定义表格显示数据的更多相关文章
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...
- Android自定义照相机实现(拍照、保存到SD卡,利用Bundle在Acitivity交换数据)
Android自定义照相机实现 近期小巫在学校有一个创新项目,也不是最近,是一个拖了很久的项目,之前一直没有去搞,最近因为要中期检查,搞得我跟小组成员一阵忙活,其实开发一款照相机软件并不太难,下面就是 ...
- 【朝花夕拾】Android自定义View篇之(八)多点触控(上)MotionEvent简介
前言 在前面的文章中,介绍了不少触摸相关的知识,但都是基于单点触控的,即一次只用一根手指.但是在实际使用App中,常常是多根手指同时操作,这就需要用到多点触控相关的知识了.多点触控是在Android2 ...
- Android自定义View4——统计图View
1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...
- (转)[原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
- Android 自定义View合集
自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...
- Android 自定义ListView
本文讲实现一个自定义列表的Android程序,程序将实现一个使用自定义的适配器(Adapter)绑定 数据,通过contextView.setTag绑定数据有按钮的ListView. 系统显示列表(L ...
- [原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
- Android 自定义ScrollView ListView 体验各种纵向滑动的需求
分类: [android 进阶之路]2014-08-31 12:59 6190人阅读 评论(10) 收藏 举报 Android自定义ScrollView纵向拖动 转载请标明出处:http: ...
随机推荐
- 给linux 授权一个可以远程登录的账户
创建用户:grant all on *.* to '; 授权:flush privileges;
- Bash Shell内建命令和保留字
Bash Shell内建命令和保留字命令含义!保留字,逻辑非:不做任何事,只做参数展开.读取文件并在shell中执行它alias设置命令或命令行别名bg将作业置于后台运行bind将关键字序列与read ...
- WP8应用上传失败查错
开发的一个应用,好久没有更新. 最近做了些修改,在设备上安装测试,没什么 问题,上传到STORE,收到反馈说有两个操作必挂. 知道了直接设备安装测试和从STORE上下载,会有些不同. http://s ...
- Quartz conTrigger 定时器表达式大全
CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 1 秒 是 0-59 , ...
- etcd第一集
网站:https://github.com/coreos/etcd 一些观点:https://yq.aliyun.com/articles/11035 1.etcd是键值存储仓库,配置共享和服务发现2 ...
- Javascript中数组与字典(即map)的使用
简述: 简单记录一下数据结构Map和数组, 其实在Javascript这种弱类型的脚本语言中,数组同时也就是字典,下面主要就是字典数组的简易使用 代码: 1. 数组中添加map <!DOCT ...
- 阿里云 crp kelude远程部署tomcat8 重启tomcat脚本
部署包路径:/usr/local/tomcat/webapps 应用服务启动脚本路径:/usr/local/tomcat/bin/restart.sh 部署授权帐号:tomcat restart.sh ...
- CSS 伪类 (Pseudo-classes)
CSS 伪类用于向某些选择器添加特殊的效果. CSS 伪类 (Pseudo-classes)实例: 超链接 本例演示如何向文档中的超链接添加不同的颜色. 超链接 2 本例演示如何向超链接添加其他样式. ...
- UICollectionView的使用
感谢下面文章作者的无私奉献 基础用法: http://www.cnblogs.com/ios8/p/iOS-UICollectionView.html http://my.oschina.net/jo ...
- 关于C#循环图片GDI+内存不足异常的记录
循环大量图片的时候会报出GDI+内存不足的问题,这个问题困扰了我很久,下面是我的几个解决方案: 使用using(){}代码块来定义需要释放内存的变量.using代码块会在代码结束的时候释放变量,但是并 ...