HtmlTagHandler

Introduction: TextView 显示 html
More: Author   ReportBugs   
Tags:

支持 TextView 默认支持的所有标签,支持自定义,取代安卓默认的 Html.TagHandler

  • 特性
    • 支持 TextView 默认支持的标签
    • 支持自定义标签,接口类似 Html.TagHandler

1 自定义标签:

/**
*  解析<span style=\"{color:#e60012}\">哈哈哈</span>
*/
public class SpanTagHandler implements HtmlTagHandler.TagHandler {

    private String fontColor = "";

    @Override
    public void handleTag(boolean open, String tag, Editable output, Attributes attrs) {

        if(tag.toLowerCase().equals("span")){
            if(open){
                //开标签,output 是空(sax 还没读到),attrs 有值
                for(int i = 0; i < attrs.getLength(); i++){
                    if(attrs.getLocalName(i).equals("style")){
                        String style = attrs.getValue(i); //{color:#e60012}
                        fontColor = style.replace("{", "").replace("}", "").replace("color", "").replace(":", "");
                    }
                }
            }else{
                //闭标签,output 有值了,attrs 没值
                output.setSpan(new ForegroundColorSpan(Color.parseColor(fontColor)), 0, output.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
}

2 html 转换成 spanned

String content = "<span style=\"{color:#e60012}\">哈哈哈</span>";
Spanned s = HtmlTagHandler.fromHtml(content, null, new SpanTagHandler());
tv.setText(s);

3 为什么要有这个库

按照安卓默认提供的方式,让 TextView 显示 html 得这样: tv_2.setText(Html.fromHtml(content, null, new Html.TagHandler()));

但是 Html.TagHandler 提供的接口是: public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader)

span 标签默认是不被支持的,所以要自己写的话,要得到 span 标签的文本和 style 属性,但 Attributes 没传出来, 反而传了个 XmlReader 对象出来,XmlReader 对象我不会用

看 android.text.Html 的源码: handleStartTag(String tag, Attributes attributes) 这里其实已经解析出属性了,为何不传出来呢???

所以 HtmlTagHandler 就干了两件事,一是拷出源码,二是更改接口,去掉 XmlReader,换成 Attributes

4 问题

如果直接解析:

String content = "呵呵呵<span style=\"{color:#e60012}\">哈哈哈</span>嘿嘿嘿";

会报错,需要处理成纯 xml 格式

content = "<html><body>" + content + "</body></html>";
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools