WebView returns bad window.innerHeight

前端 未结 4 1111
太阳男子
太阳男子 2021-02-14 01:23

I have an application that makes use of the Android WebView, as well as some JavaScript. When my WebViewClient calls onPageFinished(), I alert my JavaScript to run

相关标签:
4条回答
  • 2021-02-14 01:32

    In the end I used the values from webView.getHeight() and webView.getWidth(), adjusted them for screen density, and passed them in as arguments into a javascript method via webView.loadUrl(). This was the only way I could be sure I was getting the right values.

    Doing what was suggested in the comments above, checking for the width of 320, and the height of 240 will work great... until you run on a device with those exact dimensions.

    0 讨论(0)
  • 2021-02-14 01:50

    After some reading,another solution has occured. in java code, we can use

    new Handler().post(new Runnable(){ 
        @Override 
        public void run(){
            webview.loadUrl("...")
        }
    }); 
    

    add it to QueueMessage,and delayed the js to execute,and it works.

    0 讨论(0)
  • 2021-02-14 01:54

    This is because Javascript executes before WebView's View related initialization. And Android WebView returns a 320x240 default value to JS. Execute your JS after some time is ok, like this

    function go() {
    int height = view.getHeight();
    int width = view.getWidth();
    };
    window.setTimeout(go, 300);
    
    0 讨论(0)
  • 2021-02-14 01:59

    I used this:

    Java code in Activity:

    webView.addJavascriptInterface(new WebAppInterface(this), "Android");
    

    ...

    public class WebAppInterface {
        Context mContext;
    
        WebAppInterface(Context c) {
            mContext = c;
        }
    
        public int getWinHeight() {
          return webView.getHeight();
        }
    }
    

    JS code:

    var winHeight = Android.getWinHeight();
    
    0 讨论(0)
提交回复
热议问题