在android textview中显示<ul> <li>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11214001/
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
Show <ul> <li> in android textview
提问by aman.nepid
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 显示纯文本。如何在 textview 中格式化 ul 和 li 标签?
回答by Pavel
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()));
回答by K_Anas
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 linkfor more details
查看此链接了解更多详情
回答by ρяσ?ρ?я K
see HTML Tags Supported By TextViewboth uland litags are not Supported by android.text.Htmlclass.
看到HTML标签支持通过TextView的两个UL和李标签不被支持android.text.Html类。
SOLUTION :you can display these tags using WebViewor by TagHandler
解决方案:您可以使用WebView或TagHandler显示这些标签
for more help see this post:
如需更多帮助,请参阅此帖子:
Html List tag not working in android textview. what can i do?
回答by Taner
According to the answer below, you can not use every HTML tag here;
根据下面的答案,您不能在这里使用每个 HTML 标签;
https://stackoverflow.com/a/3150456/2275962
https://stackoverflow.com/a/3150456/2275962
But, sure, there ise a way of showing bullets in Android TextView. You can replace <li>
tags with •
(which is HTML code for bullet).
但是,当然,有一种方法可以在 Android TextView 中显示项目符号。您可以将<li>
标签替换为•
(这是项目符号的 HTML 代码)。
If you want to try other list icons, use the preferred one from the table is this link;
如果您想尝试其他列表图标,请使用表格中的首选图标是此链接;
回答by jameswoo
A really easy way to make things work for API level M (23) and lower From your HTML string you can use the following:
使 API 级别 M (23) 及更低级别工作的一种非常简单的方法从您的 HTML 字符串中,您可以使用以下内容:
private fun String.formattedHtml(): String {
// Work around for API Versions lower than N, Html.fromHtml does not render list items correctly
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
this.replace("<ul>", "")
.replace("</ul>", "")
.replace("<li>", "<p>? ")
.replace("</li>", "</p>")
} else {
this
}
}
Then, you can use:
然后,您可以使用:
Html.fromHtml("<ul><li> test </li></ul>".formattedHtml())
No need for TagHandler
for API N and up, as mentioned in https://stackoverflow.com/a/40524835, android.text.Html
supports li
and ul
tags.
不需要TagHandler
API N 及更高版本,如https://stackoverflow.com/a/40524835 中所述,android.text.Html
支持li
和ul
标记。
回答by mochadwi
In my case, I'm getting a list of String (from backend as a JSON Response), then render the HTML manually using this approach for TextView
only:
就我而言,我得到了一个字符串列表(从后端作为 JSON 响应),然后TextView
仅使用此方法手动呈现 HTML :
In Kotlin
在科特林
// ViewUtils.kt
@file:JvmName("ViewUtils")
package com.company
private fun List<String>.joinToBulletList(): String =
joinToString(prefix = "<ul>", postfix = "</ul>", separator = "\n") {
"<li> $it</li>"
}
fun TextView.renderToHtmlWithBulletList(contents: List<String>) {
val content = contents.joinToBulletList()
val html: Spannable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(content, Html.FROM_HTML_MODE_COMPACT, null, null) as Spannable
} else {
Html.fromHtml(content, null, null) as Spannable
}
text = html
}
// AnyActivity.kt
val contents = listOf("desc 1", "desc 2", "desc 3")
textViewFromXml.renderToHtmlWithBulletList(contents)
In Java
在 Java 中
// AnyActivity.java
List<String> contents = new String["desc 1", "desc 2", "desc 3"];
ViewUtils.renderToHtmlWithBulletList(textViewFromXml, contents)
Notice the @file:JvmName("ViewUtils")
it is useful to change the file name so Java code can call it ViewUtils.java
instead of ViewUtilsKt.java
. See here for more detail
请注意@file:JvmName("ViewUtils")
更改文件名很有用,因此 Java 代码可以调用它ViewUtils.java
而不是ViewUtilsKt.java
. 请参阅此处了解更多详情
P.S: Please feel free to edit this
PS:请随意编辑
回答by Shane
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;
}