//启用支持javascript
WebSettings settings = webs.getSettings();
settings.setJavaScriptEnabled(true);//设置java与js交互
//设置屏幕适应
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
settings.setBlockNetworkImage(true);
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(false);
settings.setDatabaseEnabled(true);
//防止中文乱码
settings.setDefaultTextEncodingName("UTF -8");
settings.setAppCacheEnabled(true);
settings.setDomStorageEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
wvWenzang.addJavascriptInterface(new MJavascriptInterface(this), "imagelistener");
wvWenzang.setWebChromeClient(new ChromeClient());
wvWenzang.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
loadUrl(url);
return true;
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
// handler.proceed(); // 兼容https
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// showProgress("");
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
hideProgress();
llPl.setVisibility(View.VISIBLE);
spvPinglun.setVisibility(View.VISIBLE);
}
wvWenzang.getSettings().setBlockNetworkImage(false);
addImageClickListener(view);//待网页加载完全后设置图片点击的监听方法
}
});
private void addImageClickListener(WebView webView) {
webView.loadUrl("javascript:(function(){" +
"var objs = document.getElementsByTagName(\"img\"); " +
" var array=new Array(); " + " for(var j=0;j<objs.length;j++){ " + "array[j]=objs[j].src;" + " } " +
"for(var i=0;i<objs.length;i++) " +
"{" +
" objs[i].onclick=function() " +
" { " +
" window.imagelistener.openImage(this.src,array); " +
" } " +
"}" +
"})()");
}