How to set the initial zoom/width for a webview

后端 未结 8 1475
無奈伤痛
無奈伤痛 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: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);
    }
    

提交回复
热议问题