问题:EditText控件:需要多行,且每行都有分割线隔离开,怎么办??先说说我自己的做法:
1、用底图的方法,可是这样有一个问题没有解决:滚动时,底图的分割线是不跟随字体移动的
2、重写控件,加下划线模式,也有问题没有解决:下划线只有文字存在时,才存在下划线,没有文字,就仅有一行了;再者,下划线跟字体贴得太近了,不美观寻求解决方法,谢谢了
1、用底图的方法,可是这样有一个问题没有解决:滚动时,底图的分割线是不跟随字体移动的
2、重写控件,加下划线模式,也有问题没有解决:下划线只有文字存在时,才存在下划线,没有文字,就仅有一行了;再者,下划线跟字体贴得太近了,不美观寻求解决方法,谢谢了
private Rect mRect;
private Paint mPaint; // we need this constructor for LayoutInflater
public LinedEditText(Context context, AttributeSet attrs) {
super(context, attrs); mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0x800000FF);
} @Override
protected void onDraw(Canvas canvas) {
int count = getLineCount();
Rect r = mRect;
Paint paint = mPaint; for (int i = 0; i < count; i++) {
int baseline = getLineBounds(i, r);
// 这里画的是一条水平线
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1,
paint);
} super.onDraw(canvas);
}
}你去仔细研究下,有什么不懂的,可以问我
这个是获取行数
比如说你使用这个自定义控件setText();android会让显示成几行,这样你就可以获取到可以显示成几行
super(context, attrs);
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0xFF80ADB7);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect r = mRect;
Paint paint = mPaint; Layout layout = getLayout(); if (!canvas.getClipBounds(r)) {
return;
}
float startX = r.left + MARGIN, stopX = r.right - MARGIN;
int count = layout.getLineCount();
int lineHeight = getLineHeight();
int height = getHeight() - getPaddingBottom() - getPaddingTop();
int n = height % lineHeight == 0 ? height / lineHeight : height / lineHeight + 1;
if (count < n) {
count = n;
}
for (int i = 1; i <= count; i++) {
/*int baseline = layout.getLineBaseline(i);
int bottom = layout.getLineBottom(i);
startY = stopY = (baseline + bottom + 10) / 2;
canvas.drawLine(startX, startY, stopX, stopY, paint);*/
int y = lineHeight * i;
canvas.drawLine(startX, y , stopX, y, paint);
}
}
}