我正在尝试创建一个
如果我使用额外的行间距,则光标位置与该行不正确对齐。虽然我已经实现了行间距,但是光标没有与行对齐。请查看下面图片中突出显示的红色框。
EditText
,使得每行之间的间距为20 dp
。请查看下面的图片。如果我使用额外的行间距,则光标位置与该行不正确对齐。虽然我已经实现了行间距,但是光标没有与行对齐。请查看下面图片中突出显示的红色框。
package com.example.dev_task_197_keyboard_accesory;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;
public class LineEditText extends EditText {
private Rect mRect;
private Paint mPaint;
// we need this constructor for LayoutInflater
public LineEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setColor(Color.BLUE); //SET YOUR OWN COLOR HERE
setMinLines(15);
}
@Override
protected void onDraw(Canvas canvas) {
int height = getHeight();
int line_height = getLineHeight();
int count = height / line_height;
if(getLineCount() > count){
count = getLineCount();
}
Rect r = mRect;
Paint paint = mPaint;
int baseline = getLineBounds(0, r);
for (int i = 0; i < count; i++) {
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
baseline += getLineHeight();//next line
}
// Finishes up by calling the parent method
super.onDraw(canvas);
}
}
如果需要调整行间距,请在 xml
中添加 lineSpaceExtra
和 lineSpaceMultiplier
参数。
请提出建议。