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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…