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: ...
随机推荐
- 『U3D学习』破坏神回忆图<二>技能系统
- VMware Player安装Debian系统
尝试用虚拟机来安装Debian系统,感觉这样一来安装与卸载方便,二来也可以在Linux系统安装出现问题的情况下方便在host主机上查找解决方法,同时也避免了要重新设置分区来安装Linux系统(双系统的 ...
- 求第K大数
1.排序找第K个数 2.快速排序分块 时间复杂度 2呢
- PHP 类中的魔术方法
定义: PHP类中以两个下画线“__”开头的方法被称为魔术方法. 分类: 例如:构造方法:__construct:析构方法:__destruct:动态重载:__set().__get().__call ...
- oracle数据库备份和还原
ip导出方式:exp demo/demo@127.0.0.1:1521/orcl file=f:\f.dmp full=y 备份:exp demo/demo@orcl file=f:\f.dmp f ...
- POSIX正则表达式
POSIX正则表达式规范 参考:http://en.wikipedia.org/wiki/Regular_expression POSIX正则表达式分为Basic Regular Expression ...
- DBA-mysql-字符集
查看支持的字符集:show charset; 查看现使用字符集:status; 1.在[mysqld]下添加 default-character-set=utf8 (mysql 5.1版本) char ...
- 转载:Java的接口及实例
转自:http://blog.csdn.net/liujun13579/article/details/7736116/ 一.定义 Java接口(Interface),是一系列方法的声明,是一些方法特 ...
- 获取与Url链接相关的信息
以下结果的值以此示例为基础:http://www.x2y2.com:80/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere j ...
- LeetCode "Design Twitter"
A mix of hashmap, list and heap. struct Tw { Tw(long long pts, int tid) { ts = pts; tweetid = tid; } ...