简体   繁体   中英

Scale image in webview to fit inside the parent layout height, so there is no scroll by default. (Android)

I have a WebView I'm loading with an image. There's a small static footer at the bottom, so the WebView does not take up the entire screen height. The image is never taller than it is wider, so when in portrait mode, the image loads into the WebView fine, being sized so that there is no need to scroll. But when in landscape orientation, the image height extends way beyond the WebView height, and requires the user to scroll. (I should mention I am controlling the orientation in code, based on user setting, at activity start, and I have modified the manifest.xml to prevent activity restart on screen orientation change.)

How can I get the image to fit in the WebView height-wise (as well as the width), for both portrait and landscape, so that by default the user doesn't have to scroll? I don't have access to modify the html either.

Currently I'm using

webview.getSettings().setBuiltInZoomControls(true);

webview.getSettings().setUseWideViewPort(true);

webview.getSettings().setLoadWithOverviewMode(true);

Accomplished it with this:

String data="<img src='" + imgUrl + "' style='max-height:100%' />";                 
webview.loadData(data, "text/html", "utf-8"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM