android - 如何在 TextView 中显示 HTML?

我有简单的HTML:

<h2>Title</h2><br>
<p>description here</p>

我想在 TextView 中显示 HTML 样式的文本。如何做到这一点?

最佳答案

您需要使用 Html.fromHtml()在您的 XML 字符串中使用 HTML。在您的布局 XML 中简单地引用带有 HTML 的字符串是行不通的。

这就是你在 Java 中应该做的事情

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));
} else { 
    textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
}

在 Kotlin 中:

textView.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    Html.fromHtml(html, Html.FROM_HTML_MODE_COMPACT)
} else {
    Html.fromHtml(html)
}

https://stackoverflow.com/questions/2116162/

相关文章:

javascript - 如何使用 .css() 应用 !important?

html - 表格内的文本对齐类

c# - 如何处理 ASP.NET MVC 框架中的多个提交按钮?

jquery - 按 Enter 阻止用户提交表单

html - Bootstrap 3 中的 sr-only 是什么?

html - 搜索引擎如何处理 AngularJS 应用程序?

html - 如何垂直对齐 div 中的元素?

html - 如何将我的 div 放在其容器的底部?

css - 如何使用 CSS 轻松地将

水平居中?

html - HTML5 中有浮点输入类型吗?