关于 TextView 显示富文本信息,使用 Html.fromHtml() 得到 CharSequence 对象,然后赋值给 TextView 完成显示; Html.fromHtml() 有两个常用的重载方法:
1. fromHtml(String source)
2. fromHtml(String source, ImageGetter imageGetter, TagHandler tagHandler)
区别在于第一个用来显示文字的,第二个还可以显示图片;方法一直接调用即可,没什么好说的,方法二由于要创建 ImageGetter 显示图片,并且加载网络图片需要子线程中处理,所以相关代码如下:
在子线程中创建 ImageGetter, 并调用 fromHtml(String source, ImageGetter imageGetter, TagHandler tagHandler) 方法得到 CharSequence 对象,通过 Handler 回到主线程,为 TextView 设置富文本
new Thread(() -> {
Html.ImageGetter imageGetter = source -> {
Drawable drawable = getImageFromNetwork(source);
if (drawable != null) {
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
}
return drawable;
};
CharSequence charSequence = Html.fromHtml(htmlCode, imageGetter, null);
Message message = mHandler.obtainMessage();
message.what = 0;
message.obj = charSequence;
mHandler.sendMessage(message);
}).start();
public Drawable getImageFromNetwork(String imageUrl) {
Drawable drawable;
try {
URL myFileUrl = new URL(imageUrl);
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
drawable = Drawable.createFromStream(is, null);
is.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return drawable;
}
private static class MyHandler extends Handler {
WeakReference weakReference;
MyHandler(Activity activity) {
weakReference = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
weakReference.get().txtView.setText((CharSequence) msg.obj);
weakReference.get().txtView.setClickable(true);
weakReference.get().txtView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
}
但是,一运行,GG… 文字的显示样式完全没变化, Html 里面明明设置了一些 style 属性;无奈,祭出 WebView 加载富文本;
关于 WebView、可以加载网页、本地 html 文件、竟然还可以加载 html 代码;6得飞起
1. 对 WebView 进行一些基础设置
WebSettings webSettings = webView.getSettings();
webSettings.setDisplayZoomControls(false); //隐藏webview缩放按钮
webSettings.setJavaScriptEnabled(true);//支持js
// webSettings.setBuiltInZoomControls(true); // 显示放大缩小
// webSettings.setSupportZoom(true); // 可以缩放
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// 得到 URL 可以传给应用中的某个 WebView 页面加载显示
return true;
}
});
webView.loadDataWithBaseURL(null, HtmlCode, "text/html", "UTF-8", null);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webView.loadDataWithBaseURL(null, HtmlFormat.getNewContent(htmlCode), "text/html", "UTF-8", null);
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
public class HtmlFormat {
public static String getNewContent(String htmltext) {
Document doc = Jsoup.parse(htmltext);
Elements elements = doc.getElementsByTag("img");
for (Element element : elements) {
element.attr("width", "100%").attr("height", "auto");
}
return doc.toString();
}
}