您的位置:首页 > 其它

一个TextView使用不同的颜色

2015-07-16 17:39 246 查看
一个TextView使用不同的颜色:

textview中首先需要已经有内容了

public static void ColorFormatTextView(TextView tv,int color,String textStr){
if (TextUtils.isEmpty(textStr) || tv == null
|| TextUtils.isEmpty(tv.getText())) {
return;
}
String showString = tv.getText().toString();
int startIndex = showString.indexOf(textStr);
if (startIndex < 0) {
return;
}
ForegroundColorSpan style = new ForegroundColorSpan(color);
SpannableStringBuilder formatted = new SpannableStringBuilder(
showString);
formatted.setSpan(style, startIndex, startIndex + textStr.length(),
Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
tv.setText(formatted);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: