|
|
被表白的盒饭 · <video>:视频嵌入元素 - ...· 2 月前 · |
|
|
失望的鸡蛋面 · 循环吧,代码 - 学习 Web 开发 | MDN· 2 月前 · |
|
|
苦恼的鸡蛋面 · Svelte ...· 2 月前 · |
|
|
温文尔雅的菠萝 · IE调试网页之一:F12 ...· 3 周前 · |
|
|
爱玩的登山鞋 · oracle ...· 1 年前 · |
|
|
老实的毛衣 · 使用Selenium控制当前已经打开的chr ...· 1 年前 · |
|
|
另类的路灯 · 管理从Python到redis的连接· 2 年前 · |
|
|
宽容的楼梯 · X-Frame-Options: ...· 2 年前 · |
|
|
果断的甜瓜 · oracle sql instr 用法-掘金· 2 年前 · |
Html列表标签在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>";
我将其加载到文本视图中,如下所示:
textview.setText(Html.fromHtml(str));
输出看起来像一个段落。我能做什么?有什么解决方案吗?
编辑:
webview.loadData(str,"text/html","utf-8");
如果你只需要格式化一个列表,保持它简单,在你的TextView中复制/粘贴一个unicode字符就可以达到同样的效果。
·Unicode字符'BULLET‘(U+2022)
对Aman Guatam代码的一个小修复。上面的函数在渲染换行符时有问题。例如:如果之前的
<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• ");
output.append("\n\t• ");
first = false;
} else {
first = true;
}
使用LeadingMarginSpan的不同解决方案。处理有序和无序列表以及嵌套。
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 );
}
Lord Voldermort's answer
是一个很好的起点。然而,我需要
ol
标签来显示有序列表
1. 2. 3. ....
,而不是项目符号。此外,嵌套标记需要特殊处理才能正常工作。
在我的代码中,我维护了堆栈(ParentList)来跟踪打开和关闭的
ul
和
ol
标记,并知道当前的打开标记。此外,在使用嵌套
ol
标记的情况下,使用
levelWiseCounter
来维护不同的计数。
myTextView.setText(Html.fromHtml("your string", null, new CustomTagHandler()));
。。。
private static class CustomTagHandler implements TagHandler
int level = 0;
private LinkedList<Tag> parentList = new LinkedList<DetailFragment.CustomTagHandler.Tag>();
private HashMap<Integer, Integer> levelWiseCounter = new HashMap<Integer, Integer>();
@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader)
if (tag.equalsIgnoreCase("ul") || tag.equalsIgnoreCase("ol"))
if (opening)
if (tag.equalsIgnoreCase("ul"))
parentList.push(Tag.UL);
parentList.push(Tag.OL);
level++;
if (!parentList.isEmpty())
parentList.pop();
//remove counter at that level, in any present.
levelWiseCounter.remove(level);
level--;
if (level < 0)
level = 0;
else if (tag.equalsIgnoreCase("li"))
if (opening && level > 0)
//new line check
int length = output.toString().length();
if (length > 0 && (output.toString().charAt(length - 1) == '\n'))
output.append("\n");
//add tabs as per current level of li
for (int i = 0; i < level; i++)
output.append("\t");
// append dot or numbers based on parent tag
if (Tag.UL == parentList.peek())
output.append("•");
//parent is OL. Check current level and retreive counter from levelWiseCounter
int counter = 1;
if (levelWiseCounter.get(level) == null)
levelWiseCounter.put(level, 1);
counter = levelWiseCounter.get(level) + 1;
levelWiseCounter.put(level, counter);
output.append(padInt(counter) + ".");
//trailing tab
output.append("\t");
* Add padding so that all numbers are aligned properly. Currently supports padding from 1-99.
* @param num
* @return
private static String padInt(int num)
if (num < 10)
return " " + num;
return "" + num;
private enum Tag
UL, OL
}
当然,在Android TextView中有一种显示项目符号的方式。您可以将
<li>
标记替换为
•
(这是项目符号的HTML码)。
如果您想尝试其他列表图标,请使用表格中的首选图标是此链接;
您可以简单地用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");
}
我有一个问题,我总是在@Kuitsis解决方案的列表后面得到一个空行。我在handleTag()中添加了几行代码,现在空行没有了:
@Override
public void handleTag(final boolean opening, final String tag, final Editable output, final XMLReader xmlReader) {
if (UL_TAG.equalsIgnoreCase(tag)) {
if (opening) { // handle <ul>
lists.push(new Ul());
} else { // handle </ul>
lists.pop();
if (output.length() > 0 && output.charAt(output.length() - 1) == '\n') {
output.delete(output.length() - 1, output.length());
} else if (OL_TAG.equalsIgnoreCase(tag)) {
if (opening) { // handle <ol>
lists.push(new Ol()); // use default start index of 1
} else { // handle </ol>
lists.pop();
if (output.length() > 0 && output.charAt(output.length() - 1) == '\n') {
output.delete(output.length() - 1, output.length());
} else if (LI_TAG.equalsIgnoreCase(tag)) {
if (opening) { // handle <li>
lists.peek().openItem(output);
} else { // handle </li>
lists.peek().closeItem(output, lists.size());
} else {
Log.d("TagHandler", "Found an unsupported tag " + tag);
}
这证实了卡西姆所说的话。这就是碎片化。我找到了解决这个问题的方法。我必须将
<li>
和ul重命名为自定义标记。所以:
myHTML.replaceAll("</ul>","</customTag>").replaceAll("<ul>","<customTag>");
//likewise for li
然后,在我的处理程序中,我可以查找那个customTag (它什么也不做),并让它做一些事情。
//now my handler can handle the customtags. it was ignoring them after nougat.
public class UlTagHandler implements Html.TagHandler {
//for ul in nougat and up this tagHandler is completely ignored
@Override
public void handleTag(boolean opening, String tag, Editable output,
XMLReader xmlReader) {
if (tag.equals("customtag2") && opening)
output.append("\n\t\u25CF\t");
if (tag.equals("customtag2") && !opening)
output.append("\n");
}
这应该会让它在所有版本的android上都能工作。
|
|
失望的鸡蛋面 · 循环吧,代码 - 学习 Web 开发 | MDN 2 月前 |
|
|
另类的路灯 · 管理从Python到redis的连接 2 年前 |
|
|
果断的甜瓜 · oracle sql instr 用法-掘金 2 年前 |