解决WebView加载html文本时,图片太大不能适配屏幕

为了这个问题在网上找了跟多的方法,事过之后都不可以
最后在网上找到了两种可以解决的

方法1

  • 引进依赖
compile 'org.jsoup:jsoup:1.10.1'

然后新建一个HTMLFormat.java

public class HTMLFormat {

    public static String getNewContent(String htmltext){

        Document doc= Jsoup.parse(htmltext);
        Elements elements=doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width","100%").attr("height","auto");
        }

        return doc.toString();
    }

}

最后设置

web_view.loadDataWithBaseURL(null, HTMLFormat.getNewContent(data.content), "text/html", "utf-8", null)

方法2

webSettings.setUseWideViewPort(true)
webSettings.setLoadWithOverviewMode(true)
这两个方法设置完后字体会显示的特别小,这个时候可以设置大小,就是下面这个方法
setTextZoom(200)

//        设置自适应屏幕,两者合用
        webSettings.setUseWideViewPort(true) //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true) // 缩放至屏幕的大小
        webSettings.setTextZoom(200)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容