ReplacementSpan的draw()方法是不叫 [英] ReplacementSpan's draw() method isn't called
本文介绍了ReplacementSpan的draw()方法是不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设置背景字符串这样的:
I set background in string like that:
spanString.setSpan(new BackgroundColorSpan(color), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
不过,我想增加左右填充在这样的背景下,所以我创建自定义的跨度
But I would like to increase left and right padding in this background so I created custom span
public class PaddingBackgroundSpan extends ReplacementSpan {
private int mBackgroundColor;
private int mForegroundColor;
public PaddingBackgroundSpan(int backgroundColor, int foregroundColor) {
this.mBackgroundColor = backgroundColor;
this.mForegroundColor = foregroundColor;
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
return Math.round(measureText(paint, text, start, end));
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
RectF rect = new RectF(x, top, x + measureText(paint, text, start, end), bottom);
paint.setColor(mBackgroundColor);
canvas.drawRect(rect, paint);
paint.setColor(mForegroundColor);
canvas.drawText(text, start, end, x, y, paint);
}
private float measureText(Paint paint, CharSequence text, int start, int end) {
return paint.measureText(text, start, end);
}
我用我的范围是:
I use my span by:
spanString.setSpan(new PaddingBackgroundSpan(color1, color2), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
不幸的是,我的draw()方法将不会调用。的getSize()是正确调用。
Unfortunately, my draw() method isn't called. getSize() is called correctly.
推荐答案
在情况下,任何人有这个问题,我遇到过。我所要做的是通过第二个参数到setText()方法。我的电话看起来像
In case anyone has this problem, I encountered it. What I had to do is to pass a second parameter into the setText() method. My call looked like
textView.setText(spanned, TextView.BufferType.SPANNABLE);
希望这有助于!
Hope this helps!
这篇关于ReplacementSpan的draw()方法是不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文