Android开发之文本颜色改变
TextView tv = (TextView)findViewById(R.id.tv);
String str ="我要变成红色字体,红色字体,红色字体";
String[] str2 ={"我","变","红"};
tv.setText(turnSomeWordsToRed(str,str2));
public SpannableStringBuilder turnSomeWordsToRed(String OriginStr,String[] strForRed) {
// TODO Auto-generated method stub
SpannableStringBuilder styleStr = new SpannableStringBuilder(strTitle);
for(int i=0;i<strForRed.length;i++) {
int startPosition = strTitle.indexOf(strForRed[i].trim());
int endPosition = startPosition+strForRed[i].length();
styleStr.setSpan(new ForegroundColorSpan(Color.RED), startPosition, endPosition, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
return styleStr;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。