tvReplay.setText(setTextLinkOpenByWebView(tvReplay,m_Instance,entity.getData().getReply()));
tvReplay.setMovementMethod(LinkMovementMethod.getInstance());//必须写!
setTextLinkOpenByWebView方法:onclick自行设置跳转webviewActivity
public pannableStringBuildersetTextLinkOpenByWebView(final TextView tvReplay,final Context context, String answerString) {
if (!TextUtils.isEmpty(answerString)) {
Spanned htmlString = Html.fromHtml(answerString,new MImageGetter(tvReplay, context.getApplicationContext()), null);
if (htmlStringinstanceof SpannableStringBuilder) {
SpannableStringBuilder spannableStringBuilder = (SpannableStringBuilder) htmlString;
// 取得与a标签相关的Span
Object[] objs = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), URLSpan.class);
if (null != objs && objs.length !=0) {
for (Object obj : objs) {
int start = spannableStringBuilder.getSpanStart(obj);
int end = spannableStringBuilder.getSpanEnd(obj);
if (objinstanceof URLSpan) {
//先移除这个Span,再新添加一个自己实现的Span。
URLSpan span = (URLSpan) obj;
final String url = span.getURL();
spannableStringBuilder.removeSpan(obj);
spannableStringBuilder.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
ToWebViewUtil.toWeb((Activity)context,0,url);
}
}, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
}
return spannableStringBuilder;
}
}
return new SpannableStringBuilder(answerString);
}
加载HTML片段图片:方法
public class MImageGetterimplements Html.ImageGetter {
Contextc;
TextViewcontainer;
public MImageGetter(TextView text,Context c) {
this.c = c;
this.container = text;
}
public DrawablegetDrawable(String source) {
final LevelListDrawable drawable =new LevelListDrawable();
Glide.with(c).load(source).asBitmap().into(new SimpleTarget() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
if(resource !=null) {
BitmapDrawable bitmapDrawable =new BitmapDrawable(resource);
drawable.addLevel(1, 1, bitmapDrawable);
drawable.setBounds(0, 0, resource.getWidth(),resource.getHeight());
drawable.setLevel(1);
container.invalidate();
container.setText(container.getText());
}
}
});
return drawable;
}
}