ForegroundColorSpan is not applied to ReplacementSpan

孤街浪徒 提交于 2019-11-28 04:51:17

问题


I'm trying to utilize ReplacementSpans to format the input in a EditText Field (without modifying the content):

public class SpacerSpan extends ReplacementSpan {
    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        return (int) paint.measureText(text.subSequence(start,end)+" ");
    }
    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        canvas.drawText(text.subSequence(start,end)+" ", 0, 2, x, y, paint);
    }
}

This works as expected and adds spacing after the spanned section. However, if I also apply a ForegroundColorSpan the color is not set for the spanned section:

EditText edit = (EditText) findViewById(R.id.edit_text);

SpannableString content = new SpannableString("1234567890");

ForegroundColorSpan fontColor = new ForegroundColorSpan(Color.GREEN);
SpacerSpan spacer = new SpacerSpan();
content.setSpan(fontColor, 0, content.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
content.setSpan(spacer, 4, 5, Spanned.SPAN_MARK_MARK);

edit.setText(content);

The Result looks like http://i.cubeupload.com/4Us5Zj.png

If I apply a AbsoluteSizeSpan the specified font size is also applied to the Replacement Span section. Is this the intended behavior, am I missing something, or a bug in android?


回答1:


CommonWare pointed me in the right direction. It seems like ReplacementSpans are rendered before any CharacterStyleSpan are consulted [1]

A possible (but ugly) fix is to implement a custom ForegroundColorSpan that extends MetricAffectingSpan (MetricAffectingSpans are consulted before ReplacementSpans are drawn [1]).

public class FontColorSpan extends MetricAffectingSpan {

    private int mColor;

    public FontColorSpan(int color) {
        mColor = color;
    }

    @Override
    public void updateMeasureState(TextPaint textPaint) {
        textPaint.setColor(mColor);
    }
    @Override
    public void updateDrawState(TextPaint textPaint) {
        textPaint.setColor(mColor);
    }
} 

I guess this is a bug that should be reported?

[1]http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/text/TextLine.java#936



来源:https://stackoverflow.com/questions/28323901/foregroundcolorspan-is-not-applied-to-replacementspan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!