我想在将文本显示到 TextView 之前更改文本颜色。
例如,
我从服务器收到一条文本
<b>Pratik<b/> has shared photo with <b>you</b>.
现在的要求是显示 Pratik
和 you
加粗样式和蓝色文本颜色。 我尝试了几种使用 <span>
标签的方法,但我没有清楚地显示它。
String htmlBody = "<b>Pratik<b/> has shared photo with <b>you</b>.";
String formattedBody = "<span>" + htmlBody + "</span><style>b { color: #309ce8 ; } span {font-family: 'Avenir', 'Heavy'; font-size: 17; }</style>";
SpannableString text = new SpannableString(formattedBody);
tvMessage.setText(text, TextView.BufferType.SPANNABLE); // This is not working as expected.
tvMessage.setText(Html.fromHtml(htmlBody)); // This is not working as expected.
帮助我实现这一目标。
原文由 pratik03 发布,翻译遵循 CC BY-SA 4.0 许可协议