设置一个TextView中书名标红且数字字号变大
String str = “经过分析,以下内容很适合您的宝贝学习:《虾医生》、《做一个自律的孩子》、《为什么泰国被称为“大象之邦”》建议引导宝贝学习哟!”;
tv.setText(setNumColorAndBook(str));
public static SpannableStringBuilder setNumColorAndBook(Stringstr) {
SpannableStringBuilder style=new SpannableStringBuilder(str);
for(inti=0;i<str.length();i++) {
chara=str.charAt(i);
if(a>='0' && a<='9') {
style.setSpan(new ForegroundColorSpan(Color.parseColor("#FF7862")),i,i+1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new StyleSpan(Typeface.BOLD),i,i+1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new RelativeSizeSpan(1.15f),i,i+1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
if(str.contains("《") && str.contains("》")) {
str=str.replace("、","\n");
int begin=str.indexOf("《");
StringBuilder stringBuilder=new StringBuilder(str);
StringBuilder reverse=stringBuilder.reverse();
int indexOf1=reverse.indexOf("》");
int length=str.length();
int last=length-indexOf1;
Log.e("http-颜色位置:",length+"**"+begin+"**"+last);
// SpannableString spannableString = new SpannableString(str);
style.setSpan(new ForegroundColorSpan(Color.parseColor("#FF7862")),begin,last,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(newStyleSpan(Typeface.BOLD),begin,last,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
returnstyle;
}
效果如下: