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. Java操作redis简单示例

    第一:安装Redis    首先我们要安装Redis,就像我们操作数据库一样,在操作之前肯定要先创建好数据库的环境.    Redis的下载可以百度一下,或者打开下面的下载链接:    https:/ ...

  2. 【我是老中医】codeblocks无法编译的问题解决方法

    前几天把codeblocks的文件夹移动到移动硬盘里面,结果发现从此以后代码不能编译了,当时没有注意,就改用vs写代码,发现真的不是很习惯,正好学妹也碰到这种问题问我怎么解决,然后就百度了一下. 我的 ...

  3. Exchange的AutoDiscover服务

    最近工作中涉及到Exchange的AutoDiscover服务,最初想学习后输出一个博客文章.但是最后是找到一篇中文博客文章已经非常清楚的解释了这个问题,如下: http://didda.blog.5 ...

  4. ExtJs 学习之开篇(三)Ext.grid.Panel表格中的处理

    Ext.grid.Panel Ext.create('Ext.grid.Panel',{        title:'测试表格',        width:400,        height:20 ...

  5. 依赖注入 – ASP.NET MVC 4 系列

           从 ASP.NET MVC 3.0 开始就引入了一个新概念:依赖解析器(dependence resolver).极大的增强了应用程序参与依赖注入的能力,更好的在 MVC 使用的服务和创 ...

  6. C# 创建开机启动核心代码

    #region 访问 AppSettings private void SaveAppSetting(string key, string value) { var config = Configur ...

  7. getview不执行

    <FrameLayout android:layout_width="match_parent" android:layout_height="match_pare ...

  8. linux下shell编写九九乘法表

    主要语法:类似    1x2       echo   $((1*2)) for 变量 in 值1 值2 值3 ;do linux命令或者语句done

  9. Hadoop总结篇之五---模块间是怎么驱动执行的

    在MRv1中,各个模块间驱动运行的方式是函数调用的方式.这是同步的过程,上一模块调用下一模块函数后,等待其执行.效率不高. 在MRv2中做了改进,yarn基于事件驱动的并发模型.在详细介绍前,先看下图 ...

  10. 禁用Windows窗体的关闭按钮

    1. protected override void OnFormClosing(FormClosingEventArgs e) { e.Cancel = true; base.OnFormClosi ...