原因:SpannableString 每一次都要new一个新的Span对象。不能复用。
为了少写些代码,事先创建对象备用,却发现只有后面的颜色有改变,前面的字体颜色没改变。
ForegroundColorSpan blackSpan = new ForegroundColorSpan(ContextCompat.getColor(context,R.color.tv_23262e));
ForegroundColorSpan yellowSpan= new ForegroundColorSpan(ContextCompat.getColor(context,R.color.tv_dabb84));
修改为:
private ForegroundColorSpan getYellowSpan(){
return new ForegroundColorSpan(ContextCompat.getColor(context,R.color.tv_dabb84));
}
private ForegroundColorSpan getGraySpan(){
return new ForegroundColorSpan(ContextCompat.getColor(context,R.color.tv_a3a3a3));
}
builder.setSpan(getYellowSpan(), 0, first, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(getGraySpan(), first, first+second, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(getYellowSpan(), first+second, first+second+third, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(getGraySpan(), first+second+third, first+second+third+fourth, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);