textView对安卓老版本手机Html中style的支持,自定义html

1、当html中有style标签时,一些比较老的安卓手机不支持style标签,不能够加载里面的属性,

首先了解下,textView中span的了解,先熟悉下span的用法

2、Html.fromHtml(),方法实际上也是将html标签转换成了span

3、好了废话不多说,直接上代码,大家可以查看源码了解下Html.fromHtml()的转换过程

3.1第一步

String content = “这是测试内容”;
content = content.replaceAll(“style”, “mystyle”);
//text是你自己的textView的控件
text.setText(Html.fromHtml(content,null,new HtmlTagHandler()));

3.2,在回调中解析html,然后进行修改

package com.whatsegg.egarage.util;

import android.graphics.Color;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;

import org.xml.sax.XMLReader;

import java.lang.reflect.Field;
import java.util.HashMap;

/**
 * Created by blue on 2015/8/11
 */
public class HtmlTagHandler implements Html.TagHandler {
    private static final String TAG_BLUE_FONT = "mystyle";

    private int startIndex = 0;
    private int stopIndex = 0;
    final HashMap<String, String> attributes = new HashMap<String, String>();

    @Override
    public void handleTag(boolean opening, String tag, Editable output,
                          XMLReader xmlReader) {
        processAttributes(xmlReader);

        if(tag.equalsIgnoreCase(TAG_BLUE_FONT)){
            if(opening){
                startFont(tag, output, xmlReader);
            }else{
                endFont(tag, output, xmlReader);
            }
        }

    }

    public void startFont(String tag, Editable output, XMLReader xmlReader) {
        startIndex = output.length();
    }

    public void endFont(String tag, Editable output, XMLReader xmlReader){
        stopIndex = output.length();

        String color = "#FDF695";

    //这里可以将里面的内容修改成任意你想要的格式,大小,加粗,以及一系列操作
        if(!TextUtils.isEmpty(color)){
            output.setSpan(new BackgroundColorSpan(Color.parseColor(color)), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            output.setSpan(new ForegroundColorSpan(Color.parseColor("#222222")), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }else{
            output.setSpan(new ForegroundColorSpan(0xff000000), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

    }

    private void processAttributes(final XMLReader xmlReader) {
        try {
            Field elementField = xmlReader.getClass().getDeclaredField("theNewElement");
            elementField.setAccessible(true);
            Object element = elementField.get(xmlReader);
            Field attsField = element.getClass().getDeclaredField("theAtts");
            attsField.setAccessible(true);
            Object atts = attsField.get(element);
            Field dataField = atts.getClass().getDeclaredField("data");
            dataField.setAccessible(true);
            String[] data = (String[])dataField.get(atts);
            Field lengthField = atts.getClass().getDeclaredField("length");
            lengthField.setAccessible(true);
            int len = (Integer)lengthField.get(atts);

            for(int i = 0; i < len; i++){
                attributes.put(data[i * 5 + 1], data[i * 5 + 4]);
            }
        }
        catch (Exception e) {

        }
    }

}

写的比较简单,希望对遇到这种问题的人有所帮助,以上代码经过调试没有问题

猜你喜欢

转载自blog.csdn.net/qq_36190583/article/details/86644819