Android WebView 内容图片宽度自适应


WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);//支持JS
String js = "<script type=\"text/javascript\">"+
    "var imgs = document.getElementsByTagName('img');" + // 找到img标签
    "for(var i = 0; i<imgs.length; i++){" +  // 逐个改变
    "imgs[i].style.width = '100%';" +  // 宽度改为100%
    "imgs[i].style.height = 'auto';" +
    "}" +
    "</script>";

mWebView.loadData(productBean.getDetail() + js, “text/html”, “UTF-8”);//js一定要放末尾
//mWebView.loadData(productBean.getDetail() +js,“text/html; charset=UTF-8”, null);

detial是html片段,

"detail": "<p><img src="https://xmfenxiang.oss-cn-shanghai.aliyuncs.com/editor/20191114/43631201911141256497442.jpeg" style=""/></p><p><img src="https://xmfenxiang.oss-cn-shanghai.aliyuncs.com/editor/20191114/11ea7201911141256562054.jpeg" style=""/></p><p><img src="https://xmfenxiang.oss-cn-shanghai.aliyuncs.com/editor/20191114/523b920191114125702550.jpeg" style=""/></p><p><img src="https://xmfenxiang.oss-cn-shanghai.aliyuncs.com/editor/20191114/5ff64201911141257059846.jpeg" style=""/></p><p><img src="https://xmfenxiang.oss-cn-shanghai.aliyuncs.com/editor/20191114/36be0201911141257099576.jpeg" style=""/></p><p><img src="https://xmfenxiang.oss-cn-shanghai.aliyuncs.com/editor/20191114/83790201911141257139050.jpeg" style=""/></p><p style="text-align: left;"><span style="color: rgb(63, 63, 63); font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, 微软雅黑, STHeiti, 华文细黑, sans-serif; font-size: 36px; text-align: center; white-space: normal;"><br/></span></p><p style="text-align: center;"><span style="color: rgb(63, 63, 63); font-size: 36px; text-align: center; white-space: normal;">从采摘到餐桌</span></p><p style="text-align: center;"><span style="color: rgb(63, 63, 63); font-size: 36px; text-align: center; white-space: normal;"><br/></span></p><p><video src="https://xmfenxiang.oss-cn-shanghai.aliyuncs.com/editor/ad/media1.mp4" width="100%" controls=""></video></p>"
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页