【问题标题】:Change text color of bold html text更改粗体 html 文本的文本颜色
【发布时间】:2017-12-01 05:19:02
【问题描述】:

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

例如,
我正在从服务器获取文本,例如

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

现在要求以粗体样式和蓝色文本颜色显示Pratikyou 我尝试了几种使用&lt;span&gt; 标签的方法,但我没有明确的显示方式它。

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
  • 你从服务器获取的字符串对吧?还有 标记? &lt;b&gt;Pratik&lt;b/&gt; has shared photo with &lt;b&gt;you&lt;/b&gt;.
  • 看我的帖子对你有帮助

标签: android html textview foreground


【解决方案1】:
String htmlBody = "<b style='color:blue !important;'>Pratik</b> has shared photo with <b  style='color:blue !important;'>you</b>.";

【讨论】:

  • 现在试试,我更新了。看起来有些东西正在覆盖这种风格
【解决方案2】:

您的解决方案是:

String styledText = "<b><font color='red'>Pratik</font><b/> has shared photo with <b><font color='red'>you</font></b>";

【讨论】:

    【解决方案3】:

    你必须这样使用

    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));
        }
    

    【讨论】:

      猜你喜欢
      • 2023-01-30
      • 2015-04-30
      • 2021-01-12
      • 2020-04-04
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      相关资源
      最近更新 更多