arg0:<string name="red_number">数量 %1$s</string>
</pre><pre name="code" class="java" style="background-color: rgb(255, 255, 255); ">arg1: "123456"
<span style="background-color: rgb(255, 255, 255); ">显示效果:数量</span><span style="background-color: rgb(255, 102, 102);">123456</span>
</pre><pre name="code" class="java">
/**
*
* @param context
* @param arg0 字符串
* @param arg1 数字
* @return
*/
public SpannableStringBuilder setRed(Context context, int arg0, String arg1) {
String text = String.format(context.getResources().getString(arg0),
arg1);
int index[] = new int[1];
index[0] = text.indexOf(arg1);
// index[1] = text.indexOf("18");
// index[2] = text.indexOf("银宝箱");
SpannableStringBuilder style = new SpannableStringBuilder(text);
style.setSpan(new ForegroundColorSpan(Color.RED), index[0],
index[0] + arg1.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
// style.setSpan(new ForegroundColorSpan(Color.RED), index[1],
// index[1] + 2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
// style.setSpan(new BackgroundColorSpan(Color.RED), index[2],
// index[2] + 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
return style;
}
就这样。。。android 字符串 部分变色。。