简体   繁体   English

在Android TextView中插入HTML格式的字符串(不是资源)

[英]Inserting HTML formatted (not a resource) string in android TextView

I need to plug an HTML formatted string into a TextView . 我需要将HTML格式的字符串插入TextView I'm retrieving the text from a database and when I insert it using this code 我正在从数据库中检索文本,并在使用此代码插入文本时

WebView webview = (WebView) view.findViewById(R.id.webView);
text = "<html><body>" + {text retrieved form database} + "</body></html>"
webview.loadData(text, "text/html", "utf-8");

It works but this code 可以,但是这段代码

 TextView output = (TextView) view.findViewById(R.id.simple_text);
 output.setText(Html.fromHtml(text));

strips out all HTML formatting. 去除所有HTML格式。

What is the right way to do this? 什么是正确的方法?

Start by getting rid of the <html><body> and </body></html> parts, as Html.fromHtml() does not use them. 从摆脱<html><body></body></html>部分开始,因为Html.fromHtml()不使用它们。

Then, limit your HTML to use tags that are supported by Html.fromHtml() . 然后,将HTML限制为使用Html.fromHtml()支持的标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM