当用 WebView来加载 html的字符串时: webView.loadDataWithBaseURL(serviceUrl, html, "text/html","UTF-8", null); 有时候图片会很大,宽度超过屏幕的宽度时,可以再html的文本之前加入css的样式<style> img{ max-width:100%; height:auto;} </style> 这样 图片的最大宽度就会等于webview的宽度,高度自动适应,当然 如果 <img/>标签里设置style的属性固定了宽高 就行不通了,除非把style 属性去掉
/**
* 使用正则表达式 把html标签中的style属性全部替换成""
*/
private String replaceImgStyle(String html){
String reg = "style=\"([^\"]+)\"";
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(html);
return matcher.replaceAll("");
}
//设置img标签的css样式
String imgStyle = "<style> img{ max-width:100%; height:auto;} </style>";
String html = newsData.getContent();
<span style="white-space:pre"> </span>//这个工具类用来判断字符串是否为空
if(StringUtil.isEmptyString(html)){
html ="";
}else{
html = replaceImgStyle(html);
}
html = imgStyle+html;//newsData.getContent().replaceAll("<img","<img width=" + "\'" + width + "\'");
webView.loadDataWithBaseURL(CommonConfig.WS_URL, html, "text/html",
"UTF-8", null);
还有一种问题就是直接加载网页view.load(url)
如果使用了webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);这句代码在有些手机上会变形,慎用