由于I can not adjust the edittext linespacing和https://issuetracker.google.com/issues/37009353#comment17
中提到的错误我通过自定义onTextChanged
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
/**
* Created by yccheok on 24/3/2018.
*/
public class LinedEditText extends android.support.v7.widget.AppCompatEditText {
public LinedEditText(Context context) {
super(context);
initPaint();
}
public LinedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint();
}
public LinedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
}
private void initPaint() {
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0x80000000);
}
@Override
protected void onDraw(Canvas canvas) {
int left = getLeft();
int right = getRight();
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
int height = getHeight();
int lineHeight = getLineHeight();
int count = (height-paddingTop-paddingBottom) / lineHeight;
float originalLineHeight = lineHeight / getLineSpacingMultiplier();
for (int i = 0; i < count; i++) {
float baseline = lineHeight * (i + 1) + paddingTop - mPaint.descent() - (lineHeight - originalLineHeight);
canvas.drawLine(
left + paddingLeft,
baseline,
right - paddingRight,
baseline,
mPaint
);
}
super.onDraw(canvas);
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
if (lengthBefore != lengthAfter) {
float add = getLineSpacingExtra();
float mul = getLineSpacingMultiplier();
setLineSpacing(0f, 1f);
setLineSpacing(add, mul);
}
}
private Paint mPaint = new Paint();
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<mypackage.LinedEditText
android:id="@+id/edit_text"
android:gravity="top"
android:padding="12dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:background="@android:color/transparent"
android:textSize="18sp"
android:singleLine="false"
android:lineSpacingMultiplier="2.5"
android:lineSpacingExtra="0dp"
android:inputType="textMultiLine" />
</FrameLayout>
但是,光标仍有问题。按ENTER键将光标移动到下一行时,忽略行间距乘数绘制光标。当您开始输入时,只有光标的位置才会被纠正。
请看截图
lineSpacingMultiplier
)lineSpacingMultiplier
)这是一个非常烦人的错误,谷歌尚未修复它。
我很想知道,你们有没有人遇到过坚实的解决方法?你介意与我们分享吗?
由于
答案 0 :(得分:0)
您可以尝试覆盖onKeyUp()并调用setLineSpacing()
答案 1 :(得分:0)
在编辑中覆盖onMeasure
,在可绘制游标中覆盖setBounds
。
参考:https://github.com/hanks-zyh/LineHeightEditText/issues/1#issuecomment-503476003