ama*_*pid 23 html formatting android textview
我有字符串,其中包含ul和li.我试图在textview中以HTML格式显示它们.textView.setText(Html.fromHtml(myHtmlText));
但是textview显示了纯文本.如何在textview中格式化ul和li标签?
小智 48
您可以使用Html.TagHandler.
在你的情况下,它将是这样的:
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•");
}
}
Run Code Online (Sandbox Code Playgroud)
和
textView.setText(Html.fromHtml(myHtmlText, null, new UlTagHandler()));
Run Code Online (Sandbox Code Playgroud)
K_A*_*nas 10
字符串资源中支持的标记
静态字符串资源中的标记由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>
Run Code Online (Sandbox Code Playgroud)
Html.fromHtml()支持的标签
出于某种原因,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>
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅此链接
见支持通过TextView的HTML标签 都UL和李标签不被支持android.text.Html类.
SOLUTION:您可以使用WebView或TagHandler显示这些标签
如需更多帮助,请参阅此帖子:
Html List标签在android textview中不起作用.我能做什么?