简体   繁体   English

Android Html.fromHtml() 在嵌套文本样式上无法正常工作

[英]Android Html.fromHtml() does not work correctly on nested text style

I want to display the HTML我想显示 HTML

"<html><body><font color=\"black\">
In the name of <font color=\"blue\"><b> God</b></font>
the compassionate, the merciful</font></body><html>"

in a TextView.在一个文本视图中。 So I have used Html.fromHtml() , but output is different compared with browsers.所以我使用了Html.fromHtml() ,但输出与浏览器相比有所不同。

The difference is that the word "God" does not appear in blue color in the TextView.不同之处在于“上帝”一词在 TextView 中没有以蓝色出现。

Code:代码:

textView.setText("<html><body><font color=\"black\">
  In the name of <font color=\"blue\"><b> God</b></font>
  the compassionate, the merciful</font></body><html>");

Html.fromHtml(String) does not support all HTML tags Html.fromHtml(String)不支持所有 HTML 标签

List of allowed HTML tags:允许的 HTML 标签列表:

  • br br
  • p
  • div div
  • em em
  • b
  • strong强的
  • i一世
  • dfn dfn
  • big
  • small小的
  • font字体
  • blockquote块引用
  • tt
  • a一种
  • sup
  • sub

Try out like this:像这样尝试:

textView.setText(Html.fromHtml("<html><body>In the name of<font color=blue><b> God </b></font>the compassionate, the merciful</body><html>"));

Note: Black is default color so you dont have to use <font> tag for black color.注意:黑色是默认颜色,因此您不必为黑色使用<font>标签。

if you want to use then complete tags right way like (for example for GRAY and BLUE ):如果您想以正确的方式使用完整的标签(例如,对于GRAYBLUE ):

textView.setText(Html.fromHtml("<html><body><font color=gray>In the name of </font><font color=blue><b> God </b></font><font color=gray>the compassionate, the merciful</font></body><html>"));

Looks like this issue was fixed in HtmlCompat .看起来这个问题已在HtmlCompatHtmlCompat So try using this instead of old deprecated Html :所以尝试使用它而不是旧的弃用Html

import androidx.core.text.HtmlCompat;

....

HtmlCompat.fromHtml(stringVar, HtmlCompat.FROM_HTML_MODE_LEGACY);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM