Android webview圖片過大

only_books發表於2024-06-21
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

相關文章