Html 列表标签在 android textview 中不起作用。我能做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3150400/
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
Html List tag not working in android textview. what can i do?
提问by Praveen
Html List tag not working in android TextView. This is my string content:
Html List 标签在 android TextView 中不起作用。这是我的字符串内容:
String str="A dressy take on classic gingham in a soft, textured weave of stripes that resembles twill. Take a closer look at this one.<ul><li>Trim, tailored fit for a bespoke feel</li><li>Medium spread collar, one-button mitered barrel cuffs</li><li>Applied placket with genuine mother-of-pearl buttons</li><li>;Split back yoke, rear side pleats</li><li>Made in the U.S.A. of 100% imported cotton.</li></ul>";
I loaded it in a text view like this:
我在这样的文本视图中加载它:
textview.setText(Html.fromHtml(str));
The output looks like a paragraph. What can I do? Is there any solution for it?
输出看起来像一个段落。我能做什么?有什么解决办法吗?
Edit:
编辑:
webview.loadData(str,"text/html","utf-8");
回答by Cristian
As you can see in the Html
class source code, Html.fromHtml(String)
does not support all HTML tags. In this very case, <ul>
and <li>
are not supported.
正如你在Html
类源代码中看到的,Html.fromHtml(String)
不支持所有的 HTML 标签。在这种情况下,非常,<ul>
并且<li>
不被支持。
From the source code I have built a list of allowed HTML tags:
从源代码中,我建立了一个允许的 HTML 标签列表:
br
p
div
em
b
strong
cite
dfn
i
big
small
font
blockquote
tt
monospace
a
u
sup
sub
br
p
div
em
b
strong
cite
dfn
i
big
small
font
blockquote
tt
monospace
a
u
sup
sub
So you better use WebView
and its loadDataWithBaseURL
method. Try something like this:
所以你最好使用WebView
它的loadDataWithBaseURL
方法。尝试这样的事情:
String str="<html><body>A dressy take on classic gingham in a soft, textured weave of stripes that resembles twill. Take a closer look at this one.<ul><li>Trim, tailored fit for a bespoke feel</li><li>Medium spread collar, one-button mitered barrel cuffs</li><li>Applied placket with genuine mother-of-pearl buttons</li><li>;Split back yoke, rear side pleats</li><li>Made in the U.S.A. of 100% imported cotton.</li></ul></body></html>";
webView.loadDataWithBaseURL(null, str, "text/html", "utf-8", null);
回答by Aman Gautam
I am having the same problem, what I did is overriding the default TagHandler. This one worked for me.
我遇到了同样的问题,我所做的是覆盖默认的TagHandler。这个对我有用。
public class MyTagHandler implements TagHandler {
boolean first = true;
String parent = null;
int index = 1;
@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
if (tag.equals("ul")) {
parent = "ul";
} else if (tag.equals("ol")) {
parent = "ol";
}
if (tag.equals("li")) {
if (parent.equals("ul")) {
if (first) {
output.append("\n\t?");
first = false;
} else {
first = true;
}
} else{
if (first) {
output.append("\n\t"+index+". ");
first = false;
index++;
} else {
first = true;
}
}
}
}
}
and for displaying the text...
并用于显示文本...
myTextView.setText(Html.fromHtml("<ul><li>I am an Android developer</li><li>Another Item</li></ul>", null, new MyTagHandler()));
[Edit]
[编辑]
Kuitsi has also posted an really good librarythat does the same, got it from this SO link.
Kuitsi 还发布了一个非常好的库,它可以做同样的事情,从这个 SO 链接中得到。
回答by Kuitsi
Full sample project is located at https://bitbucket.org/Kuitsi/android-textview-html-list.
Sample picture is available at https://kuitsi.bitbucket.io/stackoverflow3150400_screen.png
完整的示例项目位于https://bitbucket.org/Kuitsi/android-textview-html-list。
示例图片可在https://kuitsi.bitbucket.io/stackoverflow3150400_screen.png 获得
This solution is closest to masha's answer. Some code is also taken from inner class android.text.Html.HtmlToSpannedConverter
. It supports nested ordered and unordered lists but too long texts in ordered lists are still aligned with item number rather than text. Mixed lists (ol and ul) needs some work too. Sample project contains implementation of Html.TagHandlerwhich is passed to Html.fromHtml(String, ImageGetter, TagHandler).
此解决方案最接近masha's answer。一些代码也取自内部类android.text.Html.HtmlToSpannedConverter
。它支持嵌套的有序和无序列表,但有序列表中太长的文本仍然与项目编号而不是文本对齐。混合列表(ol 和 ul)也需要一些工作。样本项目包含实施Html.TagHandler这是传递给Html.fromHtml(字符串,ImageGetter,TagHandler) 。
Edit: For wider HTML tag support, https://github.com/NightWhistler/HtmlSpannermight also be worth trying.
编辑:对于更广泛的 HTML 标签支持,https://github.com/NightWhistler/HtmlSpanner也可能值得一试。
回答by Truong Nguyen
A small fix to Aman Guatam code. The function above has problem of rendering newline character. For example: if before <li>
tag is a <p>
tag, 2 newline characters are rendered. Here is upgraded code:
Aman Guatam 代码的一个小修复。上面的函数有渲染换行符的问题。例如:如果 before<li>
标签是一个<p>
标签,则渲染 2 个换行符。这是升级后的代码:
import org.xml.sax.XMLReader;
import android.text.Editable;
import android.text.Html.TagHandler;
public class ListTagHandler implements TagHandler {
boolean first = true;
@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
// TODO Auto-generated method stub
if (tag.equals("li")) {
char lastChar = 0;
if (output.length() > 0)
lastChar = output.charAt(output.length() - 1);
if (first) {
if (lastChar == '\n')
output.append("\t? ");
else
output.append("\n\t? ");
first = false;
} else {
first = true;
}
}
}
}
回答by kassim
WARNING
警告
as of Feb 10, 2016 android.text.Html
actually supports li
and ul
tags and uses a basic new BulletSpan()
, which means in the latest versions of Android the Html.TagHandler
solutions posted here will be ignored
截至 2016 年 2 月 10 日,android.text.Html
实际上支持li
和ul
标记并使用基本new BulletSpan()
,这意味着在最新版本的 Android 中,Html.TagHandler
此处发布的解决方案将被忽略
make sure your code handles this change in case you're expecting a BulletSpan with a larger gap than the default, you'll have to have some sort of solution that does a find/replace of the spans
确保您的代码处理此更改,以防您期望 BulletSpan 的间隙大于默认值,您必须有某种解决方案来查找/替换 span
回答by masha
Different solution using LeadingMarginSpan. Handles ordered and unordered lists as well as nesting.
使用 LeadMarginSpan 的不同解决方案。处理有序和无序列表以及嵌套。
public class ListTagHandler implements TagHandler
{
private int m_index = 0;
private List< String > m_parents = new ArrayList< String >( );
@Override
public void handleTag( final boolean opening, final String tag, Editable output, final XMLReader xmlReader )
{
if( tag.equals( "ul" ) || tag.equals( "ol" ) || tag.equals( "dd" ) )
{
if( opening )
{
m_parents.add( tag );
}
else m_parents.remove( tag );
m_index = 0;
}
else if( tag.equals( "li" ) && !opening ) handleListTag( output );
}
private void handleListTag( Editable output )
{
if( m_parents.get(m_parents.size()-1 ).equals( "ul" ) )
{
output.append( "\n" );
String[ ] split = output.toString( ).split( "\n" );
int lastIndex = split.length - 1;
int start = output.length( ) - split[ lastIndex ].length( ) - 1;
output.setSpan( new BulletSpan( 15 * m_parents.size( ) ), start, output.length( ), 0 );
}
else if( m_parents.get(m_parents.size()-1).equals( "ol" ) )
{
m_index++ ;
output.append( "\n" );
String[ ] split = output.toString( ).split( "\n" );
int lastIndex = split.length - 1;
int start = output.length( ) - split[ lastIndex ].length( ) - 1;
output.insert( start, m_index + ". " );
output.setSpan( new LeadingMarginSpan.Standard( 15 * m_parents.size( ) ), start, output.length( ), 0 );
}
}
}
回答by Naku
If you only need to format a list, keep it simple and copy/paste a unicode character in your TextView to achieve the same result.
如果您只需要格式化列表,请保持简单并在 TextView 中复制/粘贴一个 unicode 字符以获得相同的结果。
? Unicode Character 'BULLET' (U+2022)
? Unicode 字符 'BULLET' (U+2022)
回答by Charlie-Blake
I came here looking for TagHandler implementations. Both Truong Nguyen and Aman Guatam answers are very nice, but I needed a mixed version of both: I needed my solution not to overformat it and to be able to ressolve <ol>
tags, since I'm parsing something like <h3>title</h3><ol><li>item</li><li>item</li><li>item</li></ol>
.
我来这里是为了寻找 TagHandler 实现。Truong Nguyen 和 Aman Guatam 的答案都非常好,但我需要两者的混合版本:我需要我的解决方案不要过度格式化并能够解析<ol>
标签,因为我正在解析类似<h3>title</h3><ol><li>item</li><li>item</li><li>item</li></ol>
.
Here's my solution.
这是我的解决方案。
import org.xml.sax.XMLReader;
import android.text.Editable;
import android.text.Html.TagHandler;
public class MyTagHandler implements TagHandler {
boolean first = true;
String parent = null;
int index = 1;
public void handleTag(final boolean opening, final String tag,
final Editable output, final XMLReader xmlReader) {
if (tag.equals("ul")) {
parent = "ul";
index = 1;
} else if (tag.equals("ol")) {
parent = "ol";
index = 1;
}
if (tag.equals("li")) {
char lastChar = 0;
if (output.length() > 0) {
lastChar = output.charAt(output.length() - 1);
}
if (parent.equals("ul")) {
if (first) {
if (lastChar == '\n') {
output.append("\t? ");
} else {
output.append("\n\t? ");
}
first = false;
} else {
first = true;
}
} else {
if (first) {
if (lastChar == '\n') {
output.append("\t" + index + ". ");
} else {
output.append("\n\t" + index + ". ");
}
first = false;
index++;
} else {
first = true;
}
}
}
}
}
Note that, since we are resetting the index value whenever a new list starts, it WILL NOT work if you nest lists like in <ol><li>1<ol><li>1.1</li><li>1.2</li></ol><li>2</li></ol>
请注意,由于我们在新列表开始时重置索引值,因此如果您将列表嵌套在 <ol><li>1<ol><li>1.1</li><li>1.2</li></ol><li>2</li></ol>
- 1
- 1.1
- 1.2
- 2
- 1
- 1.1
- 1.2
- 2
With that code, you would get 1, 1, 2, 3
instead of 1, 1, 2, 2
.
使用该代码,您将获得1, 1, 2, 3
而不是1, 1, 2, 2
.
回答by Taner
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 sheetal
You can simply replace the "li" with unicodes
您可以简单地用 unicodes 替换“li”
@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
if (tag.equalsIgnoreCase("li")) {
if (opening) {
output.append("\u2022 ");
} else {
output.append("\n");
}
}
}