[英]Android Html.fromHtml() does not work correctly on nested text style
我想顯示 HTML
"<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()
,但輸出與瀏覽器相比有所不同。
不同之處在於“上帝”一詞在 TextView 中沒有以藍色出現。
代碼:
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>");
像這樣嘗試:
textView.setText(Html.fromHtml("<html><body>In the name of<font color=blue><b> God </b></font>the compassionate, the merciful</body><html>"));
注意:黑色是默認顏色,因此您不必為黑色使用
<font>
標簽。
如果您想以正確的方式使用完整的標簽(例如,對於GRAY
和BLUE
):
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>"));
看起來這個問題已在HtmlCompat
中HtmlCompat
。 所以嘗試使用它而不是舊的棄用Html
:
import androidx.core.text.HtmlCompat;
....
HtmlCompat.fromHtml(stringVar, HtmlCompat.FROM_HTML_MODE_LEGACY);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.