Which HTML tags are supported by Android TextView?

2019-01-13 07:29发布

Android's TextView class can display formatted text via HTML.fromHtml() as explained for example here: HTML tags in string for TextView

The TextView class can only deal with a small subset of HTML, but I do not know which tags and attributes are supported and which are not. The summary given here: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html does not seem to be correct. E.g. <div align="..."> does NOT work for me using Android 2.2

5条回答
姐就是有狂的资本
2楼-- · 2019-01-13 07:39

I noticed that this article:

https://web.archive.org/web/20171118200650/http://daniel-codes.blogspot.com/2011/04/html-in-textviews.html

lists <div> as being supported by Html.fromHtml(), but it doesn't show support for the "align" attribute.

(Other supported attributes are shown for tags on that page.)

The author says he constructed the reference by looking at code in the git repositories for Android.

Edit: Over time, it appears the list of supported tags has changed. See this later post for example: http://www.grokkingandroid.com/android-quick-tip-formatting-text-with-html-fromhtml/ .

Based on both those articles, I'd suggest that examining the source code seems to be the most reliable way to get the recent information.

查看更多
老娘就宠你
3楼-- · 2019-01-13 07:47

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.

//in string.xml file

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

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.

Output:

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

查看更多
对你真心纯属浪费
4楼-- · 2019-01-13 07:49

Looked it up for everyone searching for it.

Date: July 2017

Source: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/text/Html.java

Html.fromHtml supports:

  • p
  • ul
  • li
  • div
  • span
  • strong
  • b
  • em
  • cite
  • dfn
  • i
  • big
  • small
  • font
  • blockquote
  • tt
  • a
  • u
  • del
  • s
  • strike
  • sup
  • sub
  • h1
  • h2
  • h3
  • h4
  • h5
  • h6
  • img
  • br
查看更多
爷、活的狠高调
5楼-- · 2019-01-13 07:49
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));
     }

enter link description here

查看更多
家丑人穷心不美
6楼-- · 2019-01-13 07:51

Since it is constantly being updated, the best way to track which HTML tags are supported in Android is to check the source code of Html.java

查看更多
登录 后发表回答