先看效果图:如下
🔶方式一:
1.
String strMsg = "1.我爱<font color=\"#E01919\">我的祖国</font>";
tv.setText(Html.fromHtml(strMsg));
2.
String str1 = "2.我爱<font color= \"#E01919\"><small>我的祖国</small></font>";
tva.setText(Html.fromHtml(str1));
3.
String str2 = "3.我爱<font color=\"#E01919\"><big>我的祖国</big></font>";
tvb.setText(Html.fromHtml(str2));
4.
String str3 = "4.我爱<font color = \"#E01919\"><small><small>我的祖国</small></small></font>";
tvc.setText(Html.fromHtml(str3));
5.
String str4 = "5.我爱<font color=\"#E01919\"><big><big>我的祖国</big></big></font>";
tvd.setText(Html.fromHtml(str4));
6.
String str5 = "我的祖国";
tve.setText(Html.fromHtml("6.我爱" + "<font color=\"#E01919\">" + str5 + "</font>"));
7.
String str6 = "<font color=\"#673AB7\">7.我爱</font><font color=\"#E01919\">我的祖国</font>";
tvf.setText(Html.fromHtml(str6));
8.
String str7 = "<font color=\"#673AB7\">8.我爱</font><br><font color=\"#E01919\">我的祖国</font>";
tvz.setText(Html.fromHtml(str7));
🔶方式二:
最后一行:
String content = "上官小小明回复欧阳小小红:祖国真伟大";
SpannableStringBuilder builder = new SpannableStringBuilder(content);
//ForegroundColorSpan 为文字前景色,BackgroundColorSpan为文字背景色
ForegroundColorSpan buleSpan = new ForegroundColorSpan(Color.parseColor("#4d8ade"));
ForegroundColorSpan buleSpan1 = new ForegroundColorSpan(Color.parseColor("#4d8ade"));
ForegroundColorSpan blackSpan = new ForegroundColorSpan(Color.parseColor("#000000"));
builder.setSpan(buleSpan1, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(blackSpan, 5, 7, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
builder.setSpan(buleSpan, 7, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(blackSpan,12, 17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvh.setText(builder);