我希望在将文本颜色显示到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.
帮助我实现这一目标。
答案 0 :(得分:0)
String htmlBody = "<b style='color:blue !important;'>Pratik</b> has shared photo with <b style='color:blue !important;'>you</b>.";
答案 1 :(得分:0)
您的解决方案是:
String styledText = "<b><font color='red'>Pratik</font><b/> has shared photo with <b><font color='red'>you</font></b>";
答案 2 :(得分:0)
你必须像这样使用
public void methodName() {
String html = "Some text <b>here</b>, and some <b>there</b>";
String result = html.replace("<b>","<font color=\"#ff0000\"><b>").replace("</b>", "</b></font>");
textView.setText(Html.fromHtml(result));
}