public void showWebViewContent(Context context, String content) {
if (!TextUtils.isEmpty(content)) {
Matcher matcher = Pattern.compile("<body[^>]*>([\\s\\S]*)<\\/body>").matcher(content);
if (matcher.find()) {//通过正则表达式找出body的内容,包括body标签。下面是head内容,适配手机宽度、图片大小、文字换行
String head = "<head>" +
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
+
"<style>img{max-width: 100%; width:auto; height:auto;} body{word-break:break-all;}</style>"
+
"</head>";
content = "<html>" + head + matcher.group() + "</html>";
}
}
WebView webview = new WebView(context);
webview.getSettings().setDefaultTextEncodingName("utf-8");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
} else {
webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
}
webview.loadData(content, "text/html; charset=utf-8", "utf-8");
}