Android加载HTML背景,androidtextview显⽰html⽅法解析现在⽹络的繁盛时代,光⽂字是不能满⾜⼈们的胃⼝的,图⽚,flash,⾳频,视频就成为浏览⽹页的主流显⽰,在⼿机上也⼀样。在⼿机上显⽰从⽹络端获取的数据显⽰,⼤家很⾃然的想起两种⽅式,⼀种就是webview,⼀种就是TextView。当然webView直接显⽰html页⾯就⾏了,我主要说的TextView显⽰html内容。
⾸先,说下TextView到底⽀持那些标签呢,通过对源码的查看,发现Textview可以解析⼀部分html标签,如:
handleP(mSpannableStringBuilder);
start(mSpannableStringBuilder, new Blockquote());
} else if (tag.equalsIgnoreCase("tt")) {
start(mSpannableStringBuilder, new Monospace());
} else if (tag.equalsIgnoreCase("a")) {
startA(mSpannableStringBuilder, attributes);
} else if (tag.equalsIgnoreCase("u")) {
start(mSpannableStringBuilder, new Underline());
} else if (tag.equalsIgnoreCase("sup")) {
start(mSpannableStringBuilder, new Super());
} else if (tag.equalsIgnoreCase("sub")) {
start(mSpannableStringBuilder, new Sub());
} else if (tag.length() == 2 &&
tag.charAt(1) >= '1' && tag.charAt(1) <= '6') {
handleP(mSpannableStringBuilder);
start(mSpannableStringBuilder, new Header(tag.charAt(1) - '1'));
} else if (tag.equalsIgnoreCase("img")) {
startImg(mSpannableStringBuilder, attributes, mImageGetter);
} else if (mTagHandler != null) {
mTagHandler.handleTag(true, tag, mSpannableStringBuilder, mReader);
}
}
private void handleEndTag(String tag) {
if (tag.equalsIgnoreCase("br")) {
handleBr(mSpannableStringBuilder);
} else if (tag.equalsIgnoreCase("p")) {
handleP(mSpannableStringBuilder);
} else if (tag.equalsIgnoreCase("div")) {
handleP(mSpannableStringBuilder);
} else if (tag.equalsIgnoreCase("em")) {
end(mSpannableStringBuilder, Bold.class, new StyleSpan(Typeface.BOLD)); } else if (tag.equalsIgnoreCase("b")) {
end(mSpannableStringBuilder, Bold.class, new StyleSpan(Typeface.BOLD));
} else if (tag.equalsIgnoreCase("strong")) {
end(mSpannableStringBuilder, Italic.class, new StyleSpan(Typeface.ITALIC)); } else if (tag.equalsIgnoreCase("cite")) {
end(mSpannableStringBuilder, Italic.class, new StyleSpan(Typeface.ITALIC)); } else if (tag.equalsIgnoreCase("dfn")) {
end(mSpannableStringBuilder, Italic.class, new StyleSpan(Typeface.ITALIC)); } else if (tag.equalsIgnoreCase("i")) {
end(mSpannableStringBuilder, Italic.class, new StyleSpan(Typeface.ITALIC)); } else if (tag.equalsIgnoreCase("big")) {
end(mSpannableStringBuilder, Big.class, new RelativeSizeSpan(1.25f));
} else if (tag.equalsIgnoreCase("small")) {
end(mSpannableStringBuilder, Small.class, new RelativeSizeSpan(0.8f));
} else if (tag.equalsIgnoreCase("font")) {
endFont(mSpannableStringBuilder);
} else if (tag.equalsIgnoreCase("blockquote")) {
handleP(mSpannableStringBuilder);
end(mSpannableStringBuilder, Blockquote.class, new QuoteSpan());
} else if (tag.equalsIgnoreCase("tt")) {
end(mSpannableStringBuilder, Monospace.class,
new TypefaceSpan("monospace"));
} else if (tag.equalsIgnoreCase("a")) {
endA(mSpannableStringBuilder);
} else if (tag.equalsIgnoreCase("u")) {
end(mSpannableStringBuilder, Underline.class, new UnderlineSpan());
} else if (tag.equalsIgnoreCase("sup")) {
end(mSpannableStringBuilder, Super.class, new SuperscriptSpan());
} else if (tag.equalsIgnoreCase("sub")) {
end(mSpannableStringBuilder, Sub.class, new SubscriptSpan());
} else if (tag.length() == 2 &&
tag.charAt(1) >= '1' && tag.charAt(1) <= '6') {
textstylehandleP(mSpannableStringBuilder);
endHeader(mSpannableStringBuilder);
} else if (mTagHandler != null) {
mTagHandler.handleTag(false, tag, mSpannableStringBuilder, mReader);
}
}
通过源码可以看到,除了默认的⼀些标签,其还⽀持⾃定义标签;看下⾯代码:
else if (mTagHandler != null) {
mTagHandler.handleTag(false, tag, mSpannableStringBuilder, mReader);
}
系统会调⽤mTagHandler的handleTag⽅法。所以,我们可以实现此接⼝,来解析⾃⼰定义的标签类型。具体的,⾃⼰可以看⼀下下⾯实例:
sa.tvimg;
l.sax.XMLReader;
t.Context;
t.Intent;
Editable;
Html.TagHandler;
Spanned;
style.ClickableSpan;
import android.view.View;
import android.view.View.OnClickListener;
public class MxgsaTagHandler implements TagHandler{
private int sIndex = 0;
private int eIndex=0;
private final Context mContext;
public MxgsaTagHandler(Context context){
mContext=context;
}
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) { // TODO Auto-generated method stub
if (LowerCase().equals("mxgsa")) {
if (opening) {
sIndex=output.length();
}else {
eIndex=output.length();
output.setSpan(new MxgsaSpan(), sIndex, eIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
}
private class MxgsaSpan extends ClickableSpan implements OnClickListener{ @Override
public void onClick(View widget) {
// TODO Auto-generated method stub
//具体代码,可以是跳转页⾯,可以是弹出对话框,下⾯是跳转页⾯
mContext.startActivity(new Intent(mContext,MainActivity.class));
}
}
}
调⽤页⾯:
sa.tvimg;
import android.app.Activity;
import android.os.Bundle;
Html;
ethod.LinkMovementMethod;
import android.widget.TextView;
public class MxgsaActivity extends Activity{
private TextView tView;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(sa_activity);
findControl();
setData();
}
private void findControl() {
tView = (TextView) findViewById(R.id.tvImage);
}
private void setData() {
// TODO Auto-generated method stub
final String sText = "测试⾃定义标签:
测试⾃定义标签
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论