= Build.VERSION_CODES.KITKAT) WebView.setWebContentsDebugging_@suppre">
当前位置:   article > 正文

Android WebView常用设置及其作用记录_@suppresslint("setjavascriptenabled")

@suppresslint("setjavascriptenabled")

常用设置如下(包含英文注释及解释):

@SuppressLint("SetJavaScriptEnabled")
    private void initWebView() {
        webView = new WebView(this.getApplication());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            WebView.setWebContentsDebuggingEnabled(true);

        WebSettings webSettings = webView.getSettings();
        // webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        /**
         * Sets whether the WebView should enable support for the "viewport"
         * HTML meta tag or should use a wide viewport.
         * When the value of the setting is {@code false}, the layout width is always set to the
         * width of the WebView control in device-independent (CSS) pixels.
         * When the value is {@code true} and the page contains the viewport meta tag, the value
         * of the width specified in the tag is used. If the page does not contain the tag or
         * does not provide a width, then a wide viewport will be used.
         *
         * 是否支持 viewport 标签或宽视图,
         * fasle时,宽度为css样式设置的像素值
         * true:如果有 viewport 标签,会使用标签中声明的宽,如果没有viewport标签或者未指定具体宽度,
         * 则使用宽视图模式
         * @param use whether to enable support for the viewport meta tag
         */

        webSettings.setUseWideViewPort(true);

        /**
         * Sets whether the WebView loads pages in overview mode, that is,
         * zooms out the content to fit on screen by width. This setting is
         * taken into account when the content width is greater than the width
         * of the WebView control, for example, when {@link #getUseWideViewPort}
         * is enabled. The default is {@code false}.
         *
         * getUseWideViewPort为true时,设置此模式为true,网页内容的宽度大于WebView控件的宽度时,
         * 会缩小网页内容以适应webView的宽度
         */
        webSettings.setLoadWithOverviewMode(true);

        /**
         * Sets whether the WebView should support zooming using its on-screen zoom
         * controls and gestures. The particular zoom mechanisms that should be used
         * can be set with {@link #setBuiltInZoomControls}. This setting does not
         * affect zooming performed using the {@link WebView#zoomIn()} and
         * {@link WebView#zoomOut()} methods. The default is {@code true}.
         *
         * //是否支持缩放及显示缩放控件,即一个放大缩小的图标
         * @param support whether the WebView should support zoom
         */
        webSettings.setSupportZoom(true);
        /**
         * Sets whether the WebView should use its built-in zoom mechanisms. The
         * built-in zoom mechanisms comprise on-screen zoom controls, which are
         * displayed over the WebView's content, and the use of a pinch gesture to
         * control zooming. Whether or not these on-screen controls are displayed
         * can be set with {@link #setDisplayZoomControls}. The default is {@code false}.
         * <p>
         * The built-in mechanisms are the only currently supported zoom
         * mechanisms, so it is recommended that this setting is always enabled.
         *
         *  //是否使用内置缩放机制,包括缩放控件显示以及手势放大缩小,推荐使用
         * @param enabled whether the WebView should use its built-in zoom mechanisms
         */
        webSettings.setBuiltInZoomControls(true);
        /**
         * Sets whether the WebView should display on-screen zoom controls when
         * using the built-in zoom mechanisms. See {@link #setBuiltInZoomControls}.
         * The default is {@code true}.
         *
         *使用内置缩放机制时是否支显示放控件,加号减号图标的显示,默认显示
         * @param enabled whether the WebView should display on-screen zoom controls
         */
        webSettings.setDisplayZoomControls(false);
        //使用 localstorage 和 sessionstorage
        webSettings.setDomStorageEnabled(true);
        // webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(MIXED_CONTENT_ALWAYS_ALLOW);
        }
        // WebView是否支持多个窗口。
//        webSettings.setSupportMultipleWindows(true);

        /**
        Sets the initial scale for this WebView. 0 means default.
        The behavior for the default scale depends on the state of
        {@link WebSettings#getUseWideViewPort()} and
        {@link WebSettings#getLoadWithOverviewMode()}.
        If the content fits into the WebView control by width, then
        the zoom is set to 100%. For wide content, the behavor
        depends on the state of {@link WebSettings#getLoadWithOverviewMode()}.
        If its value is true, the content will be zoomed out to be fit
        by width into the WebView control, otherwise not.
        If initial scale is greater than 0, WebView starts with this value
        as initial scale.
        Please note that unlike the scale properties in the viewport meta tag,
         this method doesn't take the screen density into account.

        设置初始缩放比例,数字代表的百分比值,效果和WebSettings#getUseWideViewPort()以及WebSettings#getLoadWithOverviewMode()有关
        如果内容适应 webview 控件宽度,将不进行缩放,如果大于webview宽度且WebSettings#getLoadWithOverviewMode()为true,
         网页内容将会缩小以适应 webview 控件,否则不缩放。
         注:此方法不考虑屏幕像素面密度
         */
        webView.setInitialScale(200);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        //排版适应屏幕
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                progressBar.setProgress(newProgress);
                if (newProgress == 100)
                    progressBar.setVisibility(View.GONE);
                else
                    progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                toolbar.setTitle(title);
            }

        });
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Log.i("TAG", "onPageStarted: " + url);
                super.onPageStarted(view, url, favicon);
            }

            //当网页有重定向的时候会走,如百度,淘宝等,有重定向执行顺序,
            //onPageStarted -> shouldOverrideUrlLoading
            //-> onPageFinished(重定向前的地址) -> onPageStarted -> onPageFinished(重定向后)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i("TAG", "shouldOverrideUrlLoading: " + url);
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                Log.i("TAG", "onPageFinished: " + s);
                super.onPageFinished(webView, s);
            }
        });

        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        webContainer.addView(webView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        webView.loadUrl(startUrl);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156

shouldOverrideUrlLoading在首次加载有重定向,如打开网址:http://www.baidu.com的时候会重定向到 https,执行顺序如下:

onPageStarted: http://www.baidu.com/                            
shouldOverrideUrlLoading: https://m.baidu.com/?from=844b&vit=fps                                         
onPageFinished: http://www.baidu.com/                           
onPageStarted: https://m.baidu.com/?from=844b&vit=fps           
onPageFinished: https://m.baidu.com/?from=844b&vit=fps          

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

缩放控件的样子(只有放大缩小的时候才显示):

缩放控件

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/196715
推荐阅读