/**
* change a part of string color.
*
* @param string
* whole string.
* @param subString
* the sub string need modify color.
* @param color
* the the color you want the sub string display. can get like
* {@link Context#getResources()}.getColor(R.color.xxx) or use
* the system {@link Color}.xxx;
* @return a spannableStringBuilder(a string after modify substring color).
*/
public static SpannableStringBuilder modifyStrColor(String string, String subString, int color) {
String tipRed = String.format(string, subString);
int index[] = new int[1];
index[0] = tipRed.indexOf(subString);
SpannableStringBuilder style = new SpannableStringBuilder(tipRed);
style.setSpan(new ForegroundColorSpan(color), index[0],
index[0] + subString.length(),
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
return style;
} 直接使用TextView.setText该方法的返回值即可。
在TextView使用部分颜色文字
最新推荐文章于 2024-04-10 14:34:45 发布
本文介绍了一个用于修改字符串中特定子串颜色的方法。通过提供原始字符串、需改变颜色的子串及目标颜色,此方法能够返回一个带有指定颜色的SpannableStringBuilder对象。此技术适用于Android应用开发中对TextView内文字进行局部颜色调整的需求。
510

被折叠的 条评论
为什么被折叠?



