更改粗体 html 文本的文本颜色

新手上路,请多包涵

我想在将文本显示到 TextView 之前更改文本颜色。

例如,

我从服务器收到一条文本

<b>Pratik<b/> has shared photo with <b>you</b>.

现在的要求是显示 Pratikyou 加粗样式和蓝色文本颜色。 我尝试了几种使用 <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 许可协议

阅读 229
1 个回答
String htmlBody = "<b style='color:blue !important;'>Pratik</b> has shared photo with <b  style='color:blue !important;'>you</b>.";

原文由 slon 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题