-
未解决前,图片显示如下
-
解决方案
webview_pic.getSettings().setJavaScriptEnabled(true);//支持javascript
webview_pic.setWebViewClient(new myWebViewClient());
private class myWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//重置webview中img标签的图片大小
resetImg();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
/**
* 重置图片大小
**/
private void resetImg() {
webview_pic.loadUrl("javascript:(function(){" +
"var objs = document.getElementsByTagName('img'); " +
"for(var i=0;i<objs.length;i++) " +
"{"
+ "var img = objs[i]; " +
" img.style.maxWidth = '100%'; img.style.height = 'auto'; " +
"}" +
"})()");
}
- 解决后,如图所示
Q.E.D.