How to set the initial zoom/width for a webview

后端 未结 8 1366
無奈伤痛
無奈伤痛 2020-11-27 11:08

I am trying to get the WebView to have similar behavior as the android browser. The browser opens all pages in a way that tries to fit their width to the screen. However,

相关标签:
8条回答
  • 2020-11-27 11:49

    in Kotlin You can use,

    webView.isScrollbarFadingEnabled = true
    webView.setInitialScale(100)
    
    0 讨论(0)
  • 2020-11-27 11:50

    It is and old question but let me add a detail just in case more people like me arrive here.

    The excellent answer posted by Brian is not working for me in Jelly Bean. I have to add also:

    browser.setInitialScale(30);
    

    The parameter can be any percentage tha accomplishes that the resized content is smaller than the webview width. Then setUseWideViewPort(true) extends the content width to the maximum of the webview width.

    0 讨论(0)
  • 2020-11-27 11:50

    If you have the web view figured out but your images are still out of scale, the best solution I found (here) was simply prepending the document with:

    <style>img{display: inline; height: auto; max-width: 100%;}</style>
    

    As a result all images are scaled to match the web view width.

    0 讨论(0)
  • 2020-11-27 11:52

    //for images and swf videos use width as "100%" and height as "98%"

    mWebView2.getSettings().setJavaScriptEnabled(true);
    mWebView2.getSettings().setLoadWithOverviewMode(true);
    mWebView2.getSettings().setUseWideViewPort(true);
    mWebView2.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView2.setScrollbarFadingEnabled(true);
    
    0 讨论(0)
  • 2020-11-27 11:57

    I'm working with loading images for this answer and I want them to be scaled to the device's width. I find that, for older phones with versions less than API 19 (KitKat), the behavior for Brian's answer isn't quite as I like it. It puts a lot of whitespace around some images on older phones, but works on my newer one. Here is my alternative, with help from this answer: Can Android's WebView automatically resize huge images? The layout algorithm SINGLE_COLUMN is deprecated, but it works and I feel like it is appropriate for working with older webviews.

    WebSettings settings = webView.getSettings();
    
    // Image set to width of device. (Must be done differently for API < 19 (kitkat))
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        if (!settings.getLayoutAlgorithm().equals(WebSettings.LayoutAlgorithm.SINGLE_COLUMN))
            settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    } else {
        if (!settings.getLoadWithOverviewMode()) settings.setLoadWithOverviewMode(true);
        if (!settings.getUseWideViewPort()) settings.setUseWideViewPort(true);
    }
    
    0 讨论(0)
  • 2020-11-27 12:04

    I figured out why the portrait view wasn't totally filling the viewport. At least in my case, it was because the scrollbar was always showing. In addition to the viewport code above, try adding this:

        browser.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        browser.setScrollbarFadingEnabled(false);
    

    This causes the scrollbar to not take up layout space, and allows the webpage to fill the viewport.

    Hope this helps

    0 讨论(0)
提交回复
热议问题