Android TextView上受支持的html标签 [英] Supported html tags on Android TextView

查看:114
本文介绍了Android TextView上受支持的html标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要Android TextView上受支持的HTML标记的正式列表.我正在使用 textView.setText(Html.fromHtml(label)); ,但我不知道支持哪些标签.

I need an official list of supported HTML tags on Android TextView. I'm using textView.setText(Html.fromHtml(label)); but I don't know which tags are supported.

推荐答案

注意:

这是针对API 27的.不保证较早的版本支持此列表中显示的标签,而对于较新的版本,则不能删除(可以删除).

Note:

This targets API 27. Earlier versions are NOT guaranteed to support the tags seen in this list, and same for later ones (they could be removed).

Mark Herscher的评论让我开始思考:它有新标签,但是在任何地方都没有以纯文本格式进行记录.我认为HTML标记需要在某处声明,如果不在文档中,则必须在代码中明确声明.否则,代码将无法对其进行转换.显然,在某些情况下,它不会以纯文本形式出现,并且处理可与接口和覆盖的类一起使用,但是幸运的是,Android会将其保留为纯文本形式.

Mark Herscher's comment got me thinking: it there are new tags, but they're not documented in plain-text anywhere. I figured the HTML tags would need to be declared somewhere, if not in the docs, definitively in the code. Otherwise the code wouldn't be able to convert it. Obviously there are cases where it doesn't appear in plain text, and handling works with interfaces and overridden classes, but fortunately, Android keeps it in plain text.

对于将来的版本:您可以使用IntelliJ或其他某种IDE来探索Html.java的源代码,也可以始终转到AOSP站点. Html#fromHtml(String,int)调用 Html#fromHtml(String,Int,ImageGetter,TagHandler .最后一个方法创建一个 HtmlToSpannedConverter ,然后返回转换结果.深入到该代码中,我发现

For future versions: you can either use IntelliJ or some other IDE to explore the source code of Html.java, or you can always go to the AOSP site. Html#fromHtml(String, int) calls Html#fromHtml(String, Int, ImageGetter, TagHandler. The last method creates a HtmlToSpannedConverter, and returns the result of the conversion. Dug into that code, and I found this method:

private void handleStartTag(String tag, Attributes attributes) {
    if (tag.equalsIgnoreCase("br")) {
        // We don't need to handle this. TagSoup will ensure that there's a </br> for each <br>
        // so we can safely emit the linebreaks when we handle the close tag.
    } else if (tag.equalsIgnoreCase("p")) {
        startBlockElement(mSpannableStringBuilder, attributes, getMarginParagraph());
        startCssStyle(mSpannableStringBuilder, attributes);
    } else if (tag.equalsIgnoreCase("ul")) {
        startBlockElement(mSpannableStringBuilder, attributes, getMarginList());
    } else if (tag.equalsIgnoreCase("li")) {
        startLi(mSpannableStringBuilder, attributes);
    } else if (tag.equalsIgnoreCase("div")) {
        startBlockElement(mSpannableStringBuilder, attributes, getMarginDiv());
    } else if (tag.equalsIgnoreCase("span")) {
        startCssStyle(mSpannableStringBuilder, attributes);
    } else if (tag.equalsIgnoreCase("strong")) {
        start(mSpannableStringBuilder, new Bold());
    } else if (tag.equalsIgnoreCase("b")) {
        start(mSpannableStringBuilder, new Bold());
    } else if (tag.equalsIgnoreCase("em")) {
        start(mSpannableStringBuilder, new Italic());
    } else if (tag.equalsIgnoreCase("cite")) {
        start(mSpannableStringBuilder, new Italic());
    } else if (tag.equalsIgnoreCase("dfn")) {
        start(mSpannableStringBuilder, new Italic());
    } else if (tag.equalsIgnoreCase("i")) {
        start(mSpannableStringBuilder, new Italic());
    } else if (tag.equalsIgnoreCase("big")) {
        start(mSpannableStringBuilder, new Big());
    } else if (tag.equalsIgnoreCase("small")) {
        start(mSpannableStringBuilder, new Small());
    } else if (tag.equalsIgnoreCase("font")) {
        startFont(mSpannableStringBuilder, attributes);
    } else if (tag.equalsIgnoreCase("blockquote")) {
        startBlockquote(mSpannableStringBuilder, attributes);
    } else if (tag.equalsIgnoreCase("tt")) {
        start(mSpannableStringBuilder, new Monospace());
    } else if (tag.equalsIgnoreCase("a")) {
        startA(mSpannableStringBuilder, attributes);
    } else if (tag.equalsIgnoreCase("u")) {
        start(mSpannableStringBuilder, new Underline());
    } else if (tag.equalsIgnoreCase("del")) {
        start(mSpannableStringBuilder, new Strikethrough());
    } else if (tag.equalsIgnoreCase("s")) {
        start(mSpannableStringBuilder, new Strikethrough());
    } else if (tag.equalsIgnoreCase("strike")) {
        start(mSpannableStringBuilder, new Strikethrough());
    } else if (tag.equalsIgnoreCase("sup")) {
        start(mSpannableStringBuilder, new Super());
    } else if (tag.equalsIgnoreCase("sub")) {
        start(mSpannableStringBuilder, new Sub());
    } else if (tag.length() == 2 &&
            Character.toLowerCase(tag.charAt(0)) == 'h' &&
            tag.charAt(1) >= '1' && tag.charAt(1) <= '6') {
        startHeading(mSpannableStringBuilder, attributes, tag.charAt(1) - '1');
    } else if (tag.equalsIgnoreCase("img")) {
        startImg(mSpannableStringBuilder, attributes, mImageGetter);
    } else if (mTagHandler != null) {
        mTagHandler.handleTag(true, tag, mSpannableStringBuilder, mReader);
    }
}

其中确实包含所有受支持的HTML标记.此列表在将来的版本中可能会更改(它与我最初的回答相同),但是您可以自己浏览源以在以后的版本中找到它.使用上面的代码,这是当前受支持的列表:

Which does contain all of the supported HTML tags. This list might change in future versions (it did from the original answer I had), but you can dig through the source yourself to find it in later versions. With the above code, this is the currently supported list:

br
p
ul
li
div
span
strong
b
em
cite
dfn
i
big
small
font
blockquote
tt
a
u
del
s
strike 
sub
sup
img 
h1
h2
h3
h4
h5
h6

其他方法涵盖了受支持的属性(在< a href ="> 中, href 是属性):

Further methods cover supported attributes (in <a href="">, href is an attribute):


size 显然不受支持.



p, ul, and div calls startBlockElement, which gives one attrib:

text-align

对此特别值得注意的是受支持的align变量.

Something special worth noting about this is the supported align variables.

  • 中心-标准且不言自明
  • 开始-左对齐
  • end-右对齐

开始和结束在RTL布局中可能是相反的-我尚未测试.

start and end might be inverted in RTL layouts - I haven't tested.

span and p, and li calls startCssStyle, which gives access to:

text-decoration
background-color or background
color

文本装饰似乎仅限于 line-through .

这篇关于Android TextView上受支持的html标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆