Android 自定义TextView控件,用来组成表格方便数据的展示。
首先看一下效果
样式不是很好看,需要用的可以自己优化一下。
实现方式很简单。
1、自定义控件 MyTableTextView 继承 TextView 重写onDraw方法。在里面添加话边框的操作。
1 package lyf.com.mytableview; 2 3 import android.content.Context; 4 import android.graphics.Canvas; 5 import android.graphics.Color; 6 import android.graphics.Paint; 7 import android.util.AttributeSet; 8 import android.widget.TextView; 9 10 /** 11 * lyf on 2016/06/27 12 * 自定义TextView 13 */ 14 public class MyTableTextView extends TextView { 15 16 Paint paint = new Paint(); 17 18 public MyTableTextView(Context context, AttributeSet attrs) { 19 super(context, attrs); 20 int color = Color.parseColor("#80b9f2"); 21 // 为边框设置颜色 22 paint.setColor(color); 23 } 24 25 @Override 26 protected void onDraw(Canvas canvas) { 27 super.onDraw(canvas); 28 // 画TextView的4个边 29 canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint); 30 canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint); 31 canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint); 32 canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint); 33 } 34 }