关于在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 属性去掉

	/**
	 * 使用正则表达式 把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);这句代码在有些手机上会变形,慎用

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值