行之间有分隔线的edittext ,记事本/便签等风格效果

思路,查探一番后发现貌似只能自定义控件。。
继承edittext,系统绘制时会在canvas上处理好在正常状态下的EditText内容,咱们获取到行的位置信息,向画板的行的下方添加线条即可,详情见onDraw方法。

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

/** 行之间有分隔线的edittext */

public class DividerEditText extends EditText { 

private Rect mRect; 
private Paint mPaint; 

private static final int mLineColor = Color.RED; 
private static final int mLineHeight = 3; 

public DividerEditText(Context context) { 
super(context); 
init(); 
} 

public DividerEditText(Context context, AttributeSet attrs) { 
super(context, attrs); 
init(); 
} 


public DividerEditText(Context context, AttributeSet attrs, int defStyle) { 
super(context, attrs, defStyle); 
init(); 
} 

private void init() { 
mRect = new Rect(); 
mPaint = new Paint(); 
mPaint.setStyle(Paint.Style.STROKE); 
mPaint.setColor(mLineColor); 
mPaint.setStrokeWidth(mLineHeight); 
} 

protected void onDraw(android.graphics.Canvas canvas) { 
//先由系统绘制好正常的界面 
super.onDraw(canvas); 



//获取基本的位置信息 
int count = getLineCount(); 
Rect r = mRect; 
Paint paint = mPaint; 

//循环在每个行的下面绘制一条横线 
for (int i = 0; i < count - 1; i++) { 
int baseline = getLineBounds(i, r); 
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint); 
} 
}; 
} 

你可能感兴趣的:(android,EditText,记事本,便签,分隔线)