Android 环境WebView中图片自适应显示

               mWebView = (WebView) findViewById(R.id.webview);

               // 启用javascript

              mWebView.getSettings().setJavaScriptEnabled(true);


              mWebView.loadDataWithBaseURL(null, htmlContent,
"text/html", "UTF-8", null);

// 自适应图片大小
mWebView.loadUrl("javascript:(function(){" +
"var script = document.createElement('script');"
+ "script.type = 'text/javascript';"
+ "script.text = \"function ResizeImages() { "
+ "var myimg;"
+ "for(i=0;i <document.images.length;i++)" + "{"
+ "myimg = document.images[i];" + "myimg.setAttribute('style','max-width:230px;height:auto');"
+ "}" + "}\";"
+ "document.getElementsByTagName('head')[0].appendChild(script);" + 
"})()");


mWebView.loadUrl("javascript:ResizeImages()");

你可能感兴趣的:(Android 环境WebView中图片自适应显示)