EditText行间距增加问题和光标位置相对于行的问题

7
我正在尝试创建一个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 中添加 lineSpaceExtralineSpaceMultiplier 参数。

请提出建议。

enter image description here

1个回答

1

你可以在这里找到解决问题的两个可能选项。我测试了第一个选项(仅适用于API 12+),对我有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接