Android webview載入html自適應螢幕
阿新 • • 發佈:2019-01-29
webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);// 不使用快取 webView.getSettings().setUserAgentString(System.getProperty("http.agent")); webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//把html中的內容放大webview等寬的一列中webView.getSettings().setAppCacheEnabled(true); webView.getSettings().setDomStorageEnabled(true); webView.setWebViewClient(new MyWebViewClient()); webView.loadData(productDiscribeInfo, "text/html", "UTF-8");
private class MyWebViewClient extends WebViewClient { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); imgReset();//重置webview中img標籤的圖片大小 } @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'; " + "}" + "})()"); }