android 表格控件

来源:互联网 发布:c语言socket的用法 编辑:程序博客网 时间:2024/06/05 15:15

  android 没有现成的表格控件,但我们可以根据Tablayout,ListView等组合出自己的表格。废话少说,使用Tablayout自己写一个表格控件,该表格控件可以自己设置表头,动态的添加行,自定义背景颜色等。

效果图:


首先表格的的的某一项的文字可能很多,也可能是表格中有大的图片等内容,因此可能一个屏幕不够用的情况,这是必须使用水平和垂直的滚动条了,既然表格中不仅仅有文字,因此必须自己写好表格的每一行即是TabRow


KTabeView

import android.content.Context;import android.graphics.Color;import android.util.AttributeSet;import android.widget.HorizontalScrollView;import android.widget.ScrollView;import android.widget.TableLayout;import android.widget.TableRow;/* * 一个表格控件 * 宽哥 * 2013、8、7 */public class KtableView extends ScrollView {private TableLayout tableLayout; //表格布局private AddDivisionRow divisionRow;  //水平分割线//各种参数private int headColor = Color.WHITE;private int headHeight = TableRow.LayoutParams.WRAP_CONTENT;private int headtextColor = Color.BLACK;private int hedaMaxEms = 10;private int divisonColor = Color.RED;private int divisonWidth = 1;private int resId = 0;private int backgroudColor = Color.WHITE;private int cellMaxEms = 10;;private int cellTextColor = Color.BLACK;private int cellTextSize = 15;private int cellHeight = TableRow.LayoutParams.WRAP_CONTENT;public KtableView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubinitWidge();}/* * 公共接口 *//* * 设置表格的标题头 */public void setTableHeaders(String[] header){//添加分割线divisionRow = new AddDivisionRow(getContext());tableLayout.addView(divisionRow.addTableRow());AddHeadRow  row = new AddHeadRow(getContext(), header);row.setDivisonColor(divisonColor);row.setDivisonWidth(divisonWidth);row.setBackgroundColor(headColor);row.setTabRowHeight(headHeight);row.setTextMaxEms(hedaMaxEms);row.setTextClor(headtextColor);tableLayout.addView(row.addTableRow());tableLayout.addView(divisionRow.addTableRow()); //添加分割线}/* * 设置表格头的背景颜色 */public void setTableheadColor(int color){this.headColor =color;}/* * 设置表头的的高度 */public void setTableHeadHeigt(int height){this.headHeight = height;}/* * 设置表头的的字体颜色 */public void setTableHeadTextcolor(int color){this.headtextColor = color;}/* * 设置表头的的字体大小 */public void setTableHeadMaxEms(int size){this.hedaMaxEms =size;}public void setTableDivisonColor(int color){this.divisonColor = color;}public void setTableDivisonWidth(int width){this.divisonWidth = width;}/*******************内容     **************//* * 添加新的一行,目前的版本仅支持表格中由图像的下信息 * @@param objects  * 添加的内容 * @@param columns  * 表格的哪一行为图片数据 */public void  addNewRow(Object[] objects,int[] columns){AddContentsRow addContentsRow = new AddContentsRow(getContext(), objects, columns);if(resId != 0){addContentsRow.setBackground(resId);}else{addContentsRow.setBackColor(backgroudColor);}addContentsRow.setTextColor(cellTextColor);addContentsRow.setTextMaxEms(cellMaxEms);addContentsRow.setTextSize(cellTextSize);addContentsRow.setTabRowHeight(cellHeight);tableLayout.addView(addContentsRow.addTableRow());tableLayout.addView(divisionRow.addTableRow());}/* * 设置表格的背景 * @resId 背景的Id */public void setTableCellBackground(int resId){this.resId = resId;}public void setTableCellBaackgroundColor(int color){this.backgroudColor = color;}/* * 如果表格显示的是文字,设置文字每一行显示的字长 */public void setTableCellMaxEms(int length){this.cellMaxEms = length;}public void setTableCellTextSize(int size){this.cellTextSize = size;}public void setTableCellTextColor(int color){this.cellTextColor = color;}public void setTableCellHeight(int height){this.cellHeight = height;}/********************私有成员***************************//* * 界面的初始化 */private  void initWidge(){this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));//水平的滚动条HorizontalScrollView horizontalScrollView = new HorizontalScrollView(getContext());horizontalScrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));    horizontalScrollView.setFillViewport(true);    tableLayout = new TableLayout(getContext());  //表格布局   tableLayout.setLayoutParams(new HorizontalScrollView.LayoutParams( HorizontalScrollView.LayoutParams.MATCH_PARENT,HorizontalScrollView.LayoutParams.MATCH_PARENT));tableLayout.setStretchAllColumns(true);  //所有的列都可以被拉伸horizontalScrollView.addView(tableLayout);this.addView(horizontalScrollView);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {// TODO Auto-generated method stubsuper.onLayout(changed, l, t, r, b);}}

表格中每一行,包括表头行,表内容行,表格水平分割行,他们都共同继承与AddRow,,这样你想以后扩展就比较方便了

抽象类

import android.content.Context;import android.graphics.Color;import android.view.View;import android.widget.TableLayout;import android.widget.TableRow;public abstract class AddRow {protected Context context;protected int color = Color.RED;   //分隔颜色protected int width = 1;           //设置分隔线的宽的,包括垂直和水平的分割线protected int height = TableRow.LayoutParams.WRAP_CONTENT;//定义这个这一行的高度//一个默认的tabRow的布局参数protected TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);public AddRow(Context context) {this.context = context;}    abstract  public TableRow addTableRow();    /*     * 设置分割线的颜色     */public void setDivisonColor(int color){this.color = color;}/* * 设置分割线的宽度 */public void setDivisonWidth(int width){this.width = width;}    /*     * 设置tabRow的布局参数     * 当然其宽度是无法设置的,这里只能设置高度     */public void setTabRowHeight(int height){params = new TableLayout.LayoutParams(height, TableLayout.LayoutParams.WRAP_CONTENT);}/* * 水平竖直分割线 */    protected  View addDivision(){View view  = new View(context);    TableRow.LayoutParams params= new TableRow.LayoutParams(width,TableRow.LayoutParams.MATCH_PARENT);    view.setLayoutParams(params);view.setBackgroundColor(color);return view;}}
表头类

mport android.content.Context;import android.graphics.Color;import android.view.Gravity;import android.widget.TableRow;import android.widget.TextView;public class AddHeadRow extends AddRow {private String[] header;private int textColor = Color.BLACK;private int textLenghtMax = 10;          //定于每一行最多显示几个字private int cellBackgroundColor = Color.WHITE;public AddHeadRow(Context context,String[] header) {super(context);this.context = context;this.header = header;}/* * 设置字体的颜色 */public void setTextClor(int color){this.textColor = color;}/* * 设置每一行的的字符数 */public void setTextMaxEms(int lenght){this.textLenghtMax = lenght;}/* * 设置表格的背景颜色 默认为白色 */public  void setBackgroundColor(int color){this.cellBackgroundColor = color;}    @Overridepublic TableRow addTableRow(){        TableRow tableRow = new TableRow(context);    tableRow.addView(addDivision());           //添加分隔符号        tableRow.setLayoutParams(params);    for(int i = 0;i<header.length;i++){           TextView textView = new TextView(context);    TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.MATCH_PARENT);textView.setLayoutParams(params);            textView.setGravity(Gravity.CENTER|Gravity.CENTER);  //文本居中显示        textView.setTextColor(textColor); //设置文本颜色        textView.setSingleLine(false);textView.setMaxEms(textLenghtMax);        textView.setBackgroundColor(cellBackgroundColor); //设施背景颜色         textView.setText(header[i]);                tableRow.addView(textView);        tableRow.addView(addDivision());             }    return tableRow;    }}

表内容(可以包含图片)当然你也可以包含自己的内容格式,只需要继承于AddRow

import android.content.Context;import android.graphics.Color;import android.view.Gravity;import android.widget.ImageView;import android.widget.TableRow;import android.widget.TextView;/* * 添加内容 */public class AddContentsRow extends AddRow {private Object[] objects;   //表格填充内容private int[] columns;       //那些行为图片的信息?private int resId;private int backcolor = Color.WHITE; // 默认背景为白色private boolean isBackground = false;private int textMaxlength = 10;// 如果有文本信息,则文本信息的长度private int textColor = Color.BLACK;private int textSize = 18; // 文字大小 如果是文本的信息public AddContentsRow(Context context, Object[] objects, int[] columns) {super(context);// TODO Auto-generated constructor stubthis.objects = objects;this.columns = columns;}/* * 设置背景的颜色 */public void setBackground(int resId) {this.resId = resId;isBackground = true;}public void setBackColor(int color) {this.backcolor = color;isBackground = false;}public void setTextMaxEms(int size){this.textMaxlength = size;}public void setTextSize(int size){this.textSize = size;}    public void setTextColor(int color){    this.textColor = color;    }@Overridepublic TableRow addTableRow() {// TODO Auto-generated method stubTableRow tableRow = new TableRow(context);tableRow.addView(addDivision());tableRow.setLayoutParams(params);for (int i = 0; i < objects.length; i++) {if (judeColumns(i, columns) == -1) { // 说明这一行不是显示图片TextView textView = new TextView(context);textView.setClickable(true);textView.setGravity(Gravity.CENTER | Gravity.CENTER);if (isBackground) {textView.setBackgroundResource(resId);}else{textView.setBackgroundColor(backcolor);}TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.MATCH_PARENT);textView.setLayoutParams(params);textView.setSingleLine(false);textView.setMaxEms(textMaxlength);textView.setTextColor(textColor);textView.setTextSize(textSize);  //float型textView.setText((String) objects[i]);tableRow.addView(textView);// 添加分割线tableRow.addView(addDivision());} else {ImageView imageView = new ImageView(context);imageView.setBackgroundResource((Integer) objects[i]);tableRow.addView(imageView);// 添加分割线tableRow.addView(addDivision());}}return tableRow;}// 判断第几列是显示图片的private int judeColumns(int i, int[] columnsImage) {int j = columnsImage.length;for (int z = 0; z < j; z++) {if (columnsImage[z] == i) {return i;}}return -1;}}
水平分割线(分割线其实就是一个TabRow 只不过是里面只有一个view)

import android.content.Context;import android.graphics.Color;import android.view.View;import android.widget.TableRow;public class AddDivisionRow extends AddRow {public AddDivisionRow(Context context) {super(context);// TODO Auto-generated constructor stub}@Overridepublic TableRow addTableRow() {// TODO Auto-generated method stubTableRow tableRow = new TableRow(context);tableRow.setLayoutParams(params);tableRow.setBackgroundColor(color);        View view  = new View(context);    TableRow.LayoutParams params1= new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, width);    view.setLayoutParams(params1);view.setBackgroundColor(Color.RED);tableRow.addView(view);return tableRow;}}

看看主界面

import java.util.ArrayList;import java.util.List;import com.k.ktable.KtableView;import android.os.Bundle;import android.app.Activity;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);KtableView ktableView = (KtableView)findViewById(R.id.ktable);String header[] = {"姓名","年龄","爱好","任务"};Object test1[] = {"李","21","分割跟个倒萨当断失断等四大大打啊倒萨 大 大的大额的是",R.drawable.alert_reda};Object test2[] = {"李","22","b3",R.drawable.ic_launcher};Object test3[] = {"李","23","c3",R.drawable.ic_launcher};Object test4[] = {"李","24","d3",R.drawable.alert_reda};List<Object[]> list = new ArrayList<Object[]>();list.add(test1);list.add(test2);list.add(test3);list.add(test4);int[] b ={3}; //第三行显示的是图片ktableView.setTableHeaders(header);ktableView.setTableCellMaxEms(5);    ktableView.addNewRow(test1, b);    ktableView.addNewRow(test2, b);    ktableView.setTableCellBackground(R.drawable.down_up);    ktableView.addNewRow(test3, b);    ktableView.addNewRow(test4, b);    ktableView.addNewRow(test1, b);}}

在布局中使用我的控件方式

   <com.k.ktable.KtableView        android:id="@+id/ktable"       android:layout_width="match_parent"       android:layout_height="match_parent"       ></com.k.ktable.KtableView>

好了这样的表格出来了,如果需要整个完整的工程或者代码,留言给你发邮件