Android 如何在 TextView 中显示 HTML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2116162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:36:11  来源:igfitidea点击:

How to display HTML in TextView?

androidhtmlxml-parsingtextview

提问by UMAR

I have simple HTML:

我有简单的HTML

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

I want to display HTML styled text it in TextView. How to do this?

我想以 .html 格式显示 HTML 样式的文本TextView。这该怎么做?

回答by dxh

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.

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

This is what you should do:

这是你应该做的:

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>"));
}

回答by Shohan Ahmed Sijan

setText(Html.fromHtml(bodyData))is deprecatedafter api 24. Now you have to do this:

setText(Html.fromHtml(bodyData))在 api 24 之后被弃用。现在你必须这样做:

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
      tvDocument.setText(Html.fromHtml(bodyData,Html.FROM_HTML_MODE_LEGACY));
 } else {
      tvDocument.setText(Html.fromHtml(bodyData));
 }

回答by Felipe

Have a look on this: https://stackoverflow.com/a/8558249/450148

看看这个:https: //stackoverflow.com/a/8558249/450148

It is pretty good too!!

它也很不错!!

<resource>
    <string name="your_string">This is an <u>underline</u> text demo for TextView.</string>
</resources>

It works only for few tags.

它仅适用于少数标签。

回答by user1299412

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 配置它而无需在 java 代码中进行任何修改,您可能会发现这个想法很有帮助。只需从构造函数调用 init 并将文本设置为 html

public class HTMLTextView extends TextView {
    ... constructors calling init...
    private void init(){
       setText(Html.fromHtml(getText().toString()));
    }    
}

xml:

xml:

<com.package.HTMLTextView
android:text="@string/about_item_1"/>

回答by Phileo99

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:

如果您尝试从字符串资源 ID 显示 HTML,则格式可能不会显示在屏幕上。如果您遇到这种情况,请尝试使用 CDATA 标签:

strings.xml:
<string name="sample_string"><![CDATA[<h2>Title</h2><br><p>Description here</p>]]></string>

...

...

MainActivity.java:
text.setText(Html.fromHtml(getString(R.string.sample_string));

See this postfor further details.

有关更多详细信息,请参阅此帖子

回答by Rajiv Manivannan

The below code gave best result for me.

下面的代码给了我最好的结果。

TextView myTextview = (TextView) findViewById(R.id.my_text_view);
htmltext = <your html (markup) character>;
Spanned sp = Html.fromHtml(htmltext);
myTextview.setText(sp);

回答by Pedro

String value = "<html> <a href=\"http://example.com/\">example.com</a> </html>";
    SiteLink= (TextView) findViewById(R.id.textViewSite);
    SiteLink.setText(Html.fromHtml(value));
    SiteLink.setMovementMethod(LinkMovementMethod.getInstance());

回答by prom85

If you just want to display some html text and don't really need a TextView, then take a WebViewand use it like following:

如果你只是想显示一些 html 文本而不是真的需要 a TextView,那么使用 aWebView并使用它,如下所示:

String htmlText = ...;
webview.loadData(htmlText , "text/html; charset=UTF-8", null);

This does not restrict you to a few html tags either.

这也不限制您使用几个 html 标签。

回答by Rajendhiran Easu

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 部分用于 strings.xml 文件中的字符串以将 html 内容实际显示到 TextView 的最佳方法,下面的代码片段将为您提供合理的想法。

//in string.xml file
<string name="welcome_text"><![CDATA[<b>Welcome,</b> to the forthetyroprogrammers blog Logged in as:]]> %1$s.</string>

//and in Java code
String welcomStr=String.format(getString(R.string.welcome_text),username);
tvWelcomeUser.setText(Html.fromHtml(welcomStr));

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.

即使在使用 String.format 方法格式化文本后,字符串文本中的 CData 部分也能保持 html 标签数据完整。因此, Html.fromHtml(str) 工作正常,您将在欢迎消息中看到粗体文本。

Output:

输出:

Welcome, to your favorite music app store. Logged in as: username

欢迎来到您最喜欢的音乐应用商店。登录身份:用户名

回答by Teo Inke

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.

值得一提的是,方法Html.fromHtml(String source)自 API 级别 24 起已弃用。如果这是您的目标 API,则应改用Html.fromHtml(String source, int flags)