android webview图片缩放,Android WebView,缩放图像以适合屏幕

"博主分享了如何通过修改XML内容,插入`width="100%"`属性来确保网页中的图片在不同设备上自适应。使用StringBuilder操作技巧,展示了在WebView中设置HTML时如何针对`src`属性进行替换和插入的过程。"
摘要由CSDN通过智能技术生成

什么工作对我来说是这样的:我读的是,为了适应屏幕宽度,你应该添加到您的HTML或XML领域内:

width="100%"

所以我做的是,而不是缩放和缩放图像,我得到的XML,把它放在一个StringBuilder,找到src =“https://blablabla.com/image.png”是在字段并在“src”子字符串之前插入“width =”100%“”,然后y使用StringBuilder设置我的webView,mi代码如下:

public void setWebViewWithImageFit(String content){

// content is the content of the HTML or XML.

String stringToAdd = "width=\"100%\" ";

// Create a StringBuilder to insert string in the middle of content.

StringBuilder sb = new StringBuilder(content);

int i = 0;

int cont = 0;

// Check for the "src" substring, if it exists, take the index where

// it appears and insert the stringToAdd there, then increment a counter

// because the string gets altered and you should sum the length of the inserted substring

while(i != -1){

i = content.indexOf("src", i + 1);

if(i != -1) sb.insert(i + (cont * stringToAdd.length()), stringToAdd);

++cont;

}

// Set the webView with the StringBuilder: sb.toString()

WebView detailWebView = (WebView) findViewById(R.id.web_view);

detailWebView.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);

}

希望这会有所帮助,我花了几个小时才弄清楚如何解决这个问题。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值