我正在尝试在EditText中有可见的行。我对EditText with English Text的结果感到满意,但当我使用一些语言时;阿拉伯语";阿拉伯语文本每增加一行,都会比下划线低一点。你可以在下面的图片中看到问题:
❌
✔
我使用的代码如下所示:
public class UnderlinedEditText extends androidx.appcompat.widget.AppCompatEditText {
private final Rect mRect;
public Paint mPaint;
public UnderlinedEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.BLACK);
}
@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();
}
super.onDraw(canvas);
}
我尝试过setElegantTextHeight(true(,但没有效果,我认为阿拉伯语有额外的填充/高度,但我找不到解决方案。
有人能帮忙吗?
使用下一行代码即可解决:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
setFallbackLineSpacing(false);
}
问题是android会自动使用回退行间距。现在一切都很好:(