【发布时间】:2017-12-01 05:19:02
【问题描述】:
我想在将文本显示到 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.
帮助我实现这一目标。
【问题讨论】:
-
你可以在你的 html 字符串中尝试简单的 标签,然后设置为 Html.fromHtml
-
你从服务器获取的字符串对吧?还有 标记?
<b>Pratik<b/> has shared photo with <b>you</b>. -
看我的帖子对你有帮助
标签: android html textview foreground