我如何在 TextView 中解析带有从 firebaseDB 检索到的标签的 HTML 文本?无网页视图
How do i parse HTML text with tags that i retrieve from firebaseDB in a TextView? No WebView
所以我想弄清楚如何将带有标签的 HTML 文本解析为列表中的 Textview。有什么好的建议,有没有好的库,或者我应该手动编写代码来解析它。
有两个选项:
首先是使用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));
所以我想弄清楚如何将带有标签的 HTML 文本解析为列表中的 Textview。有什么好的建议,有没有好的库,或者我应该手动编写代码来解析它。
有两个选项:
首先是使用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));