如何使用我在 TextView 中从 firebaseDB 检索到的标签解析 HTML 文本?无 WebView [重复]

How do i parse HTML text with tags that i retrieve from firebaseDB in a TextView? No WebView [duplicate]

提问人:Olehcoding 提问时间:1/2/2019 最后编辑:PhantômaxxOlehcoding 更新时间:1/2/2019 访问量:232

问:

所以我试图弄清楚如何将这个带有标签的 HTML 文本解析为列表中的 Textview。任何好的建议,是否有一个好的库,或者我应该以某种方式手动编写代码来解析它。

java android 列表视图 firebase-realtime-database html 解析

评论


答:

0赞 May Rest in Peace 1/2/2019 #1

有两种选择:

首先是使用Html.fromHtml(htmlString)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
   textView.setText(Html.fromHtml(htmlString,Html.FROM_HTML_MODE_LEGACY));
} else  {
   textView.setText(Html.fromHtml(htmlString));
}

此方法中允许的标记包括:<a href="..."> <b>, <big>, <blockquote>, <br>, <cite>, <dfn> <div align="...">, <em>, <font size="..." color="..." face="..."> <h1>, <h2>, <h3>, <h4>, <h5>, <h6> <i>, <img src="...">, <p>, <small> <strike>, <strong>, <sub>, <sup>, <tt>, <u>

另一种选择是使用CDATA

定义一个字符串资源,如

<string name="stringName"><![CDATA[<html>%1$s</html>]]></string>

然后这样称呼它textView.setText(res.getString(R.string.stringName, htmlString));

评论

0赞 Olehcoding 1/2/2019
欣赏它。现在将进行测试
0赞 May Rest in Peace 1/2/2019
@Olehcoding 如果答案对您有帮助,请将其标记为正确
0赞 Olehcoding 1/2/2019
当然,伙计!