当遭遇上the fucking lingspaceExtra时,至少会出现两个让你头疼的问题:
1、当你在编辑内容的时候,你会遇到这个问题
详细的可以戳这里:
http://stackoverflow.com/questions/27421809/edittext-linespacingextra-strange-behavior
解决办法:自己重写Textview/Edittext ,并且覆写下onTextChanged方法。
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
float add = getLineSpacingExtra();
float mul = getLineSpacingMultiplier();
setLineSpacing(0f, 1f);
setLineSpacing(add, mul);
}
2、ImageSpan错位了,细心的你会发现。错位的大小恰好是lingspaceExtra。
详细的可以戳这里:
http://stackoverflow.com/questions/31539860/imagespan-align-baseline-when-textview-has-linespacing
解决办法:自己重写ImageSpan,覆盖Draw方法
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
//super.draw(canvas, text, start, end, x, top, y, bottom, paint);
Drawable drawable = getDrawable();
canvas.save();
Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
int fontHeight = fmPaint.descent - fmPaint.ascent;
int centerY = y + fmPaint.descent - fontHeight / 2;
int transY = centerY - (drawable.getBounds().bottom - drawable.getBounds().top) / 2;
canvas.translate(x, transY);
drawable.draw(canvas);
canvas.restore();
}
我这里的对其方式是:ALIGN_BASELINE
,drawable 的bounds设置为你字体的大小
d.setBounds(0, 0, dimen, dimen);