代码如下:
public class StringColor {
public static SpannableString setTextColor( @ColorInt int color, String text, String... colorText){
SpannableString builder=new SpannableString(text);
for (final String str :
colorText) {
int start=text.indexOf(str);
int end=start+str.length();
builder.setSpan(new ForegroundColorSpan(color),start,
end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return builder;
}
public static SpannableStringBuilder setLeftImageText(String text, Drawable... drawables){
SpannableStringBuilder builder=new SpannableStringBuilder();
String iconTag = "[icon]";
if (drawables!=null&&drawables.length>0){
for (int i = 0; i <drawables.length; i++) {
int start =0;
builder.append(iconTag);
int end = builder.length();
drawables[i].setBounds(0,0,50,50);
builder.setSpan(new ImageSpan(drawables[i], DynamicDrawableSpan.ALIGN_CENTER),start,
end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
return builder.append(text);
}
}