简体   繁体   English

Android FlowTextView中的HTML视图

[英]HTML View in Android FlowTextView

i have used FlowTextView library from https://github.com/ . 我已经从https://github.com/使用FlowTextView库。 but my problem is when i display content from HTML it not display properly. 但是我的问题是,当我从HTML显示内容时,它无法正确显示。

<html>
<body>

<h2>Unordered List with Default Bullets</h2>

<ul>
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ul>  

</body>
</html>

the above tags are my input, from that i must have below out put 上面的标签是我的输入,因此我必须在下面输入

Unordered List with Default Bullets 具有默认项目符号的无序列表

  • Coffee 咖啡
  • Tea
  • Milk 牛奶

but i got. 但是我知道了。

Unordered List with Default Bullets CoffeeTeaMilk 具有默认项目符号的无序列表CoffeeTeaMilk

this is my android code 这是我的Android代码

Spanned html = Html.fromHtml(content); Spanned html = Html.fromHtml(content); flowTextView.setText(html); flowTextView.setText(html);

please help me to escape from this stage. 请帮助我摆脱这一阶段。

You can do it using TagHandler 您可以使用TagHandler进行操作

myTextView.setText(Html.fromHtml("<html><body><ul><li>....</li></ul></body></html>", null, new MyTagHandler()));

Use below MyTagHandler class for reference.....you can customize it as per your need. 使用下面的MyTagHandler类作为参考.....您可以根据需要对其进行自定义。

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;
            }
        }   
    }
}
}

FlowTextView library has disadvantages. FlowTextView库具有缺点。 There is no need to do Html.fromHtml - the library does this for you. 无需执行Html.fromHtml-该库会为您执行此操作。 The view itself is the RelativeLayout and they use onDraw method to put your text there. 视图本身是RelativeLayout,它们使用onDraw方法将您的文本放在此处。 That is why the text looks not good and the links are not clickable and other problems. 这就是为什么文本看起来不好,链接不可单击以及其他问题的原因。

I would recommend to move to standard TextView and use Spans to create everything you want. 我建议移至标准TextView并使用Spans创建所需的所有内容。 Your links would be clickable, there are special spans for bullets, for text style and so on. 您的链接将是可单击的,项目符号,文本样式等都有特殊的跨度。 This is native android mechanism for text styling. 这是用于文本样式的本地android机制。 This maybe useful http://flavienlaurent.com/blog/2014/01/31/spans/ 这可能有用http://flavienlaurent.com/blog/2014/01/31/spans/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM