我有一个webview设置为:
WebView wv = (WebView) findViewById(R.id.display_post_activity_content);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
wv.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
} else {
wv.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
}
wv.loadDataWithBaseURL("", content, "text/html", "UTF-8", "");
content
是静态HTML,带有一些内联CSS。没有JavaScript。这在文本换行方面做得很棒。它还会调整较大图像的大小以适合屏幕宽度。
但是,有些内嵌图像小于设备宽度。文字环绕这些内容。
我希望它可以将所有图像(甚至是小图像)调整为宽度(保持宽高比)。是否可以使用WebView来实现?我可以使用Html.fromHtml()
和来做到这一点ImageGetter
,但是with的性能WebView
要好得多。
好的,所以我自己解决了这个问题:
// Content to be rendered
content = "<p>Some Html </p> with <img></img> tags";
// Set width and height
content = content.replace("<img", "<img height=\"auto\" width=\"100%\" ");
// Render
wv.loadDataWithBaseURL("", content, "text/html", "UTF-8", "");
和Voila,保留宽高比的全宽图像!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句