ReplacementSpan's draw() method isn't called

后端 未结 7 1992
北海茫月
北海茫月 2021-02-08 03:26

I set background in string like that:

spanString.setSpan(new BackgroundColorSpan(color), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

But I would l

7条回答
  •  逝去的感伤
    2021-02-08 04:17

    The documentation for ReplacementSpan.getSize() provides the answer:

    Returns the width of the span. Extending classes can set the height of the span by updating attributes of Paint.FontMetricsInt. If the span covers the whole text, and the height is not set, draw() will not be called for the span.

    Make sure to update Paint.FontMetricsInt with proper values to fix the problem.

    In my case, I just took the metrics from the TextView's Paint:

    @Override
    public int getSize(
            Paint paint,
            CharSequence text,
            int start,
            int end,
            Paint.FontMetricsInt fm) {
        final Paint.FontMetrics paintFontMetrics = paint.getFontMetrics();
        if (fm != null) {
            fm.ascent = (int) paintFontMetrics.ascent;
            fm.bottom = (int) paintFontMetrics.bottom;
            fm.descent = (int) paintFontMetrics.descent;
            fm.leading = (int) paintFontMetrics.leading;
            fm.top = (int) paintFontMetrics.top;
        }
        //...
    }
    

提交回复
热议问题