WebView adaptation issue summary (continually updated)

1, page width and height of the picture is too large

Sometimes, we use WebView page is not loaded on the movable terminal adapter, causing the Web for images larger than the screen size, affecting display, then we need to set the image size WebView adapting mobile end page.

By js script, reset the width and height of the img tag in the picture.

//开启javascript支持
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //重置webview中img标签的图片大小
                view.loadUrl("javascript:(function(){" +
                        "var objs = document.getElementsByTagName('img'); " +
                        "for(var i=0;i<objs.length;i++) " +
                        "{"
                        + "var img = objs[i]; " +
                        " img.style.maxWidth = '100%'; img.style.height = 'auto'; " +
                        "}" +
                        "})()");
            }
        });

2, Https mixed with Http request issues

In Android5.0 and above systems, WebView loaded when the link is beginning Https, but the links inside the content, such as pictures Http link, this time, the picture will load does not come out, because from the beginning Android5.0, no default WebView allows simultaneous loading Https and Http, if you want to change, we need to configure mixed mode Webview load the content of a total of three modes as follows:

  • MIXED_CONTENT_NEVER_ALLOW: Webview does not allow a secure site (https) to load insecure content sites (http), for example, pictures https web content is http link. ( Recommended App Using this model, because it is more secure )
  • MIXED_CONTENT_ALWAYS_ALLOW: In this mode, WebView is a non-secure site can load content (Http) in a secure site (Https) years. ( This pattern is likely to cause safety problems, do not use this mode as much as possible )
  • MIXED_CONTENT_COMPATIBILITY_MODE: In this mode, when it comes to hybrid content, WebView will try to compatibility with the latest Web browser style. Some insecure content (Http) can be loaded onto a secure site (Https), and other types of content will be blocked. These type of content is allowed to be loaded or blocking can be changed with the different versions, and there is no clear definition. ( This mode is mainly used for rendering can not control content on the App inside, but they want to run in a safe environment )
     

3, long-press copy prohibit WebView

  • HTML is prohibited by
//直接在HTML Style里面加入以下代码即可
<style type="text/css">
        *{
          -webkit-touch-callout:none;
          -webkit-user-select:none;
          -khtml-user-select:none;
          -moz-user-select:none;
          -ms-user-select:none;
          user-select:none;
        }
        input,textarea {
          -webkit-user-select:auto;
          margin: 0px;
          padding: 0px;
          outline: none;
        }
</style>
  • By prohibiting WebView
webview.setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        return true;
        }
    });

 

Published 57 original articles · won praise 26 · views 40000 +

Guess you like

Origin blog.csdn.net/Ever69/article/details/104547027