如何在TextView中显示HTML?
我有简单的 HTML:
<h2>Title</h2><br>
<p>description here</p>
我想在 TextView
中显示 HTML 样式的文本。如何做到这一点?
I have simple HTML:
<h2>Title</h2><br>
<p>description here</p>
I want to display HTML styled text it in TextView
. How to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(29)
您需要使用
Html。 fromHtml()
在 XML 字符串中使用 HTML。简单地在布局 XML 中引用带有 HTML 的字符串是行不通的。应该执行的操作:
这是您在 Java和 Kotlin 中
You need to use
Html.fromHtml()
to use HTML in your XML Strings. Simply referencing a String with HTML in your layout XML will not work.This is what you should do in Java
And in Kotlin:
setText(Html.fromHtml(bodyData)) 在 api 24 之后已弃用。现在您必须执行以下操作:
setText(Html.fromHtml(bodyData)) is deprecated after api 24. Now you have to do this:
看看这个: https://stackoverflow.com/a/8558249/450148
这也相当不错!
它仅适用于少数标签。
Have a look on this: https://stackoverflow.com/a/8558249/450148
It is pretty good too!!
It works only for few tags.
如果您希望能够通过 xml 配置它而不需要对 java 代码进行任何修改,您可能会发现这个想法很有帮助。只需从构造函数中调用 init 并将文本设置为 html
xml:
If you want to be able to configure it through xml without any modification in java code you may find this idea helpful. Simply you call init from constructor and set the text as html
xml:
如果您尝试从字符串资源 ID 显示 HTML,格式可能不会显示在屏幕上。如果您遇到这种情况,请尝试使用 CDATA 标记:
...
请参阅此帖子了解更多详细信息。
If you are trying to show HTML from a string resource id, the formatting may not show up on screen. If that is happening to you, try using CDATA tags instead:
...
See this post for further details.
我知道这个问题很老了。这里的其他答案建议
Html.fromHtml()
方法。我建议您使用HtmlCompat.fromHtml()
来自androidx.core.text.HtmlCompat
包。因为这是Html
类的向后兼容版本。示例代码:
通过这种方式,您可以避免Android API版本检查,并且易于使用(单行解决方案)。
I know this question is old. Other answers here suggesting
Html.fromHtml()
method. I suggest you to useHtmlCompat.fromHtml()
fromandroidx.core.text.HtmlCompat
package. As this is backward compatible version ofHtml
class.Sample code:
By this way you can avoid Android API version check and it's easy to use (single line solution).
下面的代码给了我最好的结果。
The below code gave best result for me.
如果您只想显示一些 html 文本并且实际上并不需要
TextView
,那么使用WebView
并按如下方式使用它:这并不限制您使用一些html 标签也可以。
If you just want to display some html text and don't really need a
TextView
, then take aWebView
and use it like following:This does not restrict you to a few html tags either.
使用 strings.xml 文件中的字符串的 CData 部分来将 html 内容实际显示到 TextView 的最佳方法是,下面的代码片段将为您提供公平的想法。
即使在使用 String.format 方法格式化文本后,字符串文本中的 CData 部分也能保持 html 标签数据不变。因此, Html.fromHtml(str) 工作正常,您将在欢迎消息中看到粗体文本。
输出:
The best approach to use CData sections for the string in strings.xml file to get a actual display of the html content to the TextView the below code snippet will give you the fair idea.
CData section in string text keeps the html tag data intact even after formatting text using String.format method. So, Html.fromHtml(str) works fine and you’ll see the bold text in Welcome message.
Output:
值得一提的是,该方法 Html.fromHtml( String source) 从 API 级别 24 开始已弃用。如果这是您的目标 API,您应该使用 Html.fromHtml(String source, int flags) 代替。
It's worth mentioning that the method Html.fromHtml(String source) is deprecated as of API level 24. If that's your target API, you should use Html.fromHtml(String source, int flags) instead.
创建了 Kotlin 扩展来将 html 从 String 转换 -
Created Kotlin extensions to convert html from String -
我还想建议以下项目: https://github.com/NightWhistler/HtmlSpanner
使用率几乎与默认的 android 转换器相同:
在我开始自己实现 html 到 spannable 转换器之后发现它,因为标准 Html.fromHtml 在渲染控制方面没有提供足够的灵活性,甚至无法使用 ttf 中的自定义字体
I would like also to suggest following project: https://github.com/NightWhistler/HtmlSpanner
Usage is almost the same as default android converter:
Found it after I already started by own implementation of html to spannable converter, because standard Html.fromHtml does not provide enough flexibility over rendering control and even no possibility to use custom fonts from ttf
通过各种答案建议使用 Html 框架类,如此处建议的那样,但不幸的是,该类在不同版本的 Android 中具有不同的行为以及各种未解决的错误,如问题 214637,14778、235128 和75953。
因此,您可能希望使用兼容性库来跨 Android 版本标准化和反向移植 Html 类,其中包括更多元素和样式的回调:
虽然它与框架的 Html 类类似,但需要进行一些签名更改才能允许更多回调。以下是 GitHub 页面上的示例:
It has been suggested through various answers to use the Html framework class as suggested here, but unfortunately this class has different behavior in different versions of Android and various unaddressed bugs, as demonstrated in issues 214637, 14778, 235128 and 75953.
You may therefore want to use a compatibility library to standardize and backport the Html class across Android versions which includes more callbacks for elements and styling:
While it is similar to the framework's Html class, some signature changes were required to allow more callbacks. Here's the sample from the GitHub page:
简单地使用
simply use
简单使用
Html.fromHtml("html string")
。这会起作用。如果字符串具有像这样的标签,那么就会出现空格。但我们无法消除这些空间。如果您仍然想删除空格,则可以删除字符串中的标签,然后将字符串传递给方法
Html.fromHtml("html string");
。此外,通常这些字符串来自服务器(动态),但不常见,如果在这种情况下,将字符串按原样传递给方法比尝试从字符串中删除标签更好。Simple use
Html.fromHtml("html string")
. This will work. If the string has tags like<h1>
then spaces will come. But we cannot eliminate those spaces. If you still want to remove the spaces, then you can remove the tags in the string and then pass the string to the methodHtml.fromHtml("html string");
. Also generally these strings come from server(dynamic) but not often, if it is the case better to pass the string as it is to the method than try to remove the tags from the string.如果您在项目中使用
androidx.
* 类,则应使用HtmlCompat.fromHtml(text, flag)
。该方法的来源是:
使用
HtmlCompat.fromHtml
比使用Html.fromHtml
更好,因为代码更少 - 只有一行代码,推荐使用它。If you use
androidx.
* classes in your project, you should useHtmlCompat.fromHtml(text, flag)
.Source of the method is:
It is better to use
HtmlCompat.fromHtml
thanHtml.fromHtml
as there is less code- only one line of code, and it's recommended way to use it.我已经使用网络视图实现了这一点。就我而言,我必须从 URL 加载图像以及文本视图中的文本,这对我有用。
I have implemented this using web view. In my case i have to load image from URL along with the text in text view and this works for me.
创建一个全局方法,例如:
您也可以在 Activity/Fragment 中使用它,例如:
Make a global method like:
You can also use it in your Activity/Fragment like:
人们建议使用 TextView、WebView 和各种解决方案的子类。我想知道为什么没有人提到一个简单的 绑定适配器。
所以你的 TextView xml 看起来像
People have suggested subclass for TextView, WebView and all sorts of solutions. I wonder why nobody mentioned a simple binding adapter.
So your TextView xml will look like
每当您编写自定义文本视图时,基本的 HTML 设置文本功能都会从某些设备中消失。
所以我们需要执行以下附加步骤 make is work
Whenever you write custom text view basic HTML set text feature will be get vanished form some of the devices.
So we need to do following addtional steps make is work
只需使用:
Simply use:
上述答案的更新
update of answer above
使用下面的代码来获得解决方案:
Utility Method
Use below code to get the solution:
Utitilty Method
您可以使用简单的 Kotlin 扩展函数,如下所示:
用法:
You can use simple Kotlin extension function like this:
And usage:
我可以建议一个有点老套但仍然天才的解决方案吗!我从这篇文章中得到了这个想法并进行了修改对于安卓。基本上,您使用
WebView
并在可编辑的 div 标记中插入要显示和编辑的 HTML。这样,当用户点击WebView
时,键盘就会出现并允许编辑。您只需添加一些 JavaScript 即可恢复编辑后的 HTML,瞧!这是代码:
这里是作为 Gist 的组件。
注意:我不需要原始解决方案中的高度变化回调,因此此处缺少该回调,但如果需要,您可以轻松添加它。
May I suggest a somewhat hacky but still genius solution! I got the idea from this article and adapted it for Android. Basically you use a
WebView
and insert the HTML you want to show and edit in an editable div tag. This way when the user taps theWebView
the keyboard appears and allows editing. They you just add some JavaScript to get back the edited HTML and voila!Here is the code:
And here is the component as a Gist.
Note: I didn't need the height change callback from the original solution so that's missing here but you can easily add it if needed.
您可以使用
HtmlDsl
< 为 AndroidTextView
构建有效的 HTML /a> Github 上的库:https://github.com/jaredrummler/HtmlDsl。该库提供语法糖,通过仅支持由 Android 呈现的元素和属性,使代码更易于理解且不易出错。
创建一些 HTML 的示例:
Android
TextView
支持的 HTML 元素:You can build valid HTML for Android
TextView
using theHtmlDsl
library on Github: https://github.com/jaredrummler/HtmlDsl.The library provides syntactic sugar to make the code more understandable and less error-prone by only supporting elements and attributes that are rendered by Android.
Example creating some HTML:
Supported HTML elements for Android
TextView
:使用 BindingAdapter:
}
用法:
Using BindingAdapter:
}
usage: