Android webview图片适应屏幕问题

当用 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 属性去掉

[java]  view plain  copy
  1. /** 
  2.  * 使用正则表达式 把html标签中的style属性全部替换成"" 
  3.  */  
  4. private String replaceImgStyle(String html){  
  5.     String reg = "style=\"([^\"]+)\"";  
  6.     Pattern pattern = Pattern.compile(reg);  
  7.     Matcher matcher = pattern.matcher(html);  
  8.     return matcher.replaceAll("");  
  9. }  

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //设置img标签的css样式  
  2.         String imgStyle = "<style> img{ max-width:100%; height:auto;} </style>";  
  3.         String html  = newsData.getContent();  
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space:pre">        </span>//这个工具类用来判断字符串是否为空  
  2.         if(StringUtil.isEmptyString(html)){  
  3.             html ="";  
  4.         }else{  
  5.             html = replaceImgStyle(html);  
  6.         }  
  7.         html = imgStyle+html;//newsData.getContent().replaceAll("<img","<img width=" + "\'" + width + "\'");  
  8.         webView.loadDataWithBaseURL(CommonConfig.WS_URL, html, "text/html",  
  9.                 "UTF-8"null);  


还有一种问题就是直接加载网页view.load(url)

如果使用了webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);这句代码在有些手机上会变形,慎用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值