Android webview图片过大

private class ArticleWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//重置webview中img标签的图片大小
imgReset();
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

/**
* 对图片进行重置大小,宽度就是手机屏幕宽度,高度根据宽度比便自动缩放
**/
private void imgReset() {
webview.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'; " + "}" + "})()");
}
webview.getSettings().setJavaScriptEnabled(true);//支持javascript
webview.setWebViewClient(new ArticleWebViewClient());


参考:https://www.yisu.com/jc/362279.html
posted @ 2024-06-21 13:25  only_books  阅读(7)  评论(0编辑  收藏  举报