EditView变形 使得每行都有一个下划线

 1     /**
2 * A custom EditText that draws lines between each line of text that is displayed.
3 * 这是一个自定义的EditView被画很多行再每两行的中间
4 */
5 public static class LinedEditText extends EditText {
6 /*
7 * 一个是矩形 一个是装绘 主要显示画图的样式
8 */
9 private Rect mRect;
10 private Paint mPaint;
11
12 // we need this constructor for LayoutInflater
13 /*
14 * 构造函数
15 */
16 public LinedEditText(Context context, AttributeSet attrs) {
17 super(context, attrs);
18
19 mRect = new Rect();
20 mPaint = new Paint();
21 mPaint.setStyle(Paint.Style.STROKE);
22 mPaint.setColor(0x8FF00FF);
23 }
24 /**
25 * 这个是具体的画法
26 *
27 * (non-Javadoc)
28 * @see android.widget.TextView#onDraw(android.graphics.Canvas)
29 */
30 @Override
31 protected void onDraw(Canvas canvas) {
32 int count = getLineCount();
33 Rect r = mRect;
34 Paint paint = mPaint;
35
36 for (int i = 0; i < count; i++) {
37 int baseline = getLineBounds(i, r);
38
39 canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
40 }
41
42 super.onDraw(canvas);
43 }
44 }

关键就是找到他的基准线然后再canvas上面画出来就行

原文地址:https://www.cnblogs.com/Acmen/p/2206966.html