在android textview中显示。

时间:2023-01-14 15:23:56

I have String with ul and li in it. And I am trying to show them in HTML formatting in textview.
textView.setText(Html.fromHtml(myHtmlText));
But textview shows the plain text. How can I have the ul and li tags formatted in textview?

我有带ul和li的字符串。我试着在textview的HTML格式中显示它们。textView.setText(Html.fromHtml(myHtmlText));但是textview显示的是纯文本。如何让ul和li标签在textview中格式化?

5 个解决方案

#1


35  

You can use Html.TagHandler.

您可以使用Html.TagHandler。

In your case it will be like this:

你的情况是这样的:

public class UlTagHandler implements Html.TagHandler{
    @Override
    public void handleTag(boolean opening, String tag, Editable output,
                          XMLReader xmlReader) {
            if(tag.equals("ul") && !opening) output.append("\n");
            if(tag.equals("li") && opening) output.append("\n\t•");
    }
}

and

textView.setText(Html.fromHtml(myHtmlText, null, new UlTagHandler()));

#2


9  

Tags Supported in String Resources

在字符串资源中支持的标记。

Tags in static string resources are parsed by android.content.res.StringBlock, which is a hidden class. I've looked through the class and determined which tags are supported:

静态字符串资源中的标记由android.content.res解析。StringBlock,它是一个隐藏的类。我查看了这个类并确定了支持哪些标记:

<a> (supports attributes "href")
<annotation>
<b>
<big>
<font> (supports attributes "height", "size", "fgcolor" and "bicolor", as integers)
<i>
<li>
<marquee>
<small>
<strike>
<sub>
<sup>
<tt>
<u>

Tags Supported by Html.fromHtml()

标签由Html.fromHtml()

For some reason, Html.fromHtml() handles a different set of of tags than static text supports. Here's a list of the tags (gleaned from Html.java's source code):

出于某种原因,Html.fromHtml()处理的是一组不同的标签,而不是静态文本支持。这里有一个标签列表(从Html中收集的)。java的源代码):

<a> (supports attribute "href")
<b>
<big>
<blockquote>
<br>
<cite>
<dfn>
<div>
<em>
<font> (supports attributes "color" and "face")
<i>
<img> (supports attribute "src". Note: you have to include an ImageGetter to handle retrieving a Drawable for this tag)
<p>
<small>
<strong>
<sub>
<sup>
<tt>
<u>

see this link for more details

有关更多细节,请参见此链接。

#3


6  

see HTML Tags Supported By TextView both ul and li tags are not Supported by android.text.Html class.

查看由TextView支持的HTML标签,ul和li标签都不支持android.text。Html类。

SOLUTION : you can display these tags using WebView or by TagHandler

解决方案:您可以使用WebView或TagHandler来显示这些标记。

for more help see this post:

更多的帮助请看这篇文章:

Html List tag not working in android textview. what can i do?

在android textview中不工作的Html列表标记。我能做什么?

#4


4  

According to the answer below, you can not use every HTML tag here;

根据下面的答案,你不能在这里使用每个HTML标签;

https://*.com/a/3150456/2275962

https://*.com/a/3150456/2275962

But, sure, there ise a way of showing bullets in Android TextView. You can replace <li> tags with &#149; (which is HTML code for bullet).

但是,当然,在Android TextView中有一种显示子弹的方法。您可以用•替换

  • 标签;(这是子弹的HTML代码)。

  • 标签;(这是子弹的HTML代码)。
  • If you want to try other list icons, use the preferred one from the table is this link;

    如果您想尝试其他列表图标,请从表中使用首选项为这个链接;

    http://www.ascii-code.com/

    http://www.ascii-code.com/

    #5


    0  

    Im my case my data is coming down from an API call. I still needed the output to match the format of my app so I wrap the HTML string in formatting tags to display in my WebView:

    我的数据来自于一个API调用。我仍然需要输出来匹配我的应用程序的格式,所以我将HTML字符串包装在格式标签中以显示在我的WebView中:

    public static String formatHtmlString(String stringIn) {
    
        String format = "<html>"
                + "  <head>"
                + "    <style type='text/css'>"
                + "      body { "
                + "           font-family: \"HelveticaNeue-Light\", \"Helvetica Neue Light\"; "
                + "           font-size:14px;" + "           color:#000;"
                + "      }" + "      b { " + "           font-size:14px;"
                + "       }" + "      i { " + "           font-size:10px;"
                + "           color:#333333;" + "           opacity:0.75;"
                + "       }" + "    </style>" + "  </head>" + "  <body>"
                + stringIn + "</body>" + "</html>";
    
        return format;
    }
    

    #1


    35  

    You can use Html.TagHandler.

    您可以使用Html.TagHandler。

    In your case it will be like this:

    你的情况是这样的:

    public class UlTagHandler implements Html.TagHandler{
        @Override
        public void handleTag(boolean opening, String tag, Editable output,
                              XMLReader xmlReader) {
                if(tag.equals("ul") && !opening) output.append("\n");
                if(tag.equals("li") && opening) output.append("\n\t•");
        }
    }
    

    and

    textView.setText(Html.fromHtml(myHtmlText, null, new UlTagHandler()));
    

    #2


    9  

    Tags Supported in String Resources

    在字符串资源中支持的标记。

    Tags in static string resources are parsed by android.content.res.StringBlock, which is a hidden class. I've looked through the class and determined which tags are supported:

    静态字符串资源中的标记由android.content.res解析。StringBlock,它是一个隐藏的类。我查看了这个类并确定了支持哪些标记:

    <a> (supports attributes "href")
    <annotation>
    <b>
    <big>
    <font> (supports attributes "height", "size", "fgcolor" and "bicolor", as integers)
    <i>
    <li>
    <marquee>
    <small>
    <strike>
    <sub>
    <sup>
    <tt>
    <u>
    

    Tags Supported by Html.fromHtml()

    标签由Html.fromHtml()

    For some reason, Html.fromHtml() handles a different set of of tags than static text supports. Here's a list of the tags (gleaned from Html.java's source code):

    出于某种原因,Html.fromHtml()处理的是一组不同的标签,而不是静态文本支持。这里有一个标签列表(从Html中收集的)。java的源代码):

    <a> (supports attribute "href")
    <b>
    <big>
    <blockquote>
    <br>
    <cite>
    <dfn>
    <div>
    <em>
    <font> (supports attributes "color" and "face")
    <i>
    <img> (supports attribute "src". Note: you have to include an ImageGetter to handle retrieving a Drawable for this tag)
    <p>
    <small>
    <strong>
    <sub>
    <sup>
    <tt>
    <u>
    

    see this link for more details

    有关更多细节,请参见此链接。

    #3


    6  

    see HTML Tags Supported By TextView both ul and li tags are not Supported by android.text.Html class.

    查看由TextView支持的HTML标签,ul和li标签都不支持android.text。Html类。

    SOLUTION : you can display these tags using WebView or by TagHandler

    解决方案:您可以使用WebView或TagHandler来显示这些标记。

    for more help see this post:

    更多的帮助请看这篇文章:

    Html List tag not working in android textview. what can i do?

    在android textview中不工作的Html列表标记。我能做什么?

    #4


    4  

    According to the answer below, you can not use every HTML tag here;

    根据下面的答案,你不能在这里使用每个HTML标签;

    https://*.com/a/3150456/2275962

    https://*.com/a/3150456/2275962

    But, sure, there ise a way of showing bullets in Android TextView. You can replace <li> tags with &#149; (which is HTML code for bullet).

    但是,当然,在Android TextView中有一种显示子弹的方法。您可以用•替换

  • 标签;(这是子弹的HTML代码)。

  • 标签;(这是子弹的HTML代码)。
  • If you want to try other list icons, use the preferred one from the table is this link;

    如果您想尝试其他列表图标,请从表中使用首选项为这个链接;

    http://www.ascii-code.com/

    http://www.ascii-code.com/

    #5


    0  

    Im my case my data is coming down from an API call. I still needed the output to match the format of my app so I wrap the HTML string in formatting tags to display in my WebView:

    我的数据来自于一个API调用。我仍然需要输出来匹配我的应用程序的格式,所以我将HTML字符串包装在格式标签中以显示在我的WebView中:

    public static String formatHtmlString(String stringIn) {
    
        String format = "<html>"
                + "  <head>"
                + "    <style type='text/css'>"
                + "      body { "
                + "           font-family: \"HelveticaNeue-Light\", \"Helvetica Neue Light\"; "
                + "           font-size:14px;" + "           color:#000;"
                + "      }" + "      b { " + "           font-size:14px;"
                + "       }" + "      i { " + "           font-size:10px;"
                + "           color:#333333;" + "           opacity:0.75;"
                + "       }" + "    </style>" + "  </head>" + "  <body>"
                + stringIn + "</body>" + "</html>";
    
        return format;
    }