Scroll Webview inside a Scroll View

后端 未结 4 1887
心在旅途
心在旅途 2021-01-12 19:57

What I have:
Right now I have a Scroll view as a parent. Inside this scroll view, I am using a WebView that loads a URL and then shows text in it. Here

4条回答
  •  滥情空心
    2021-01-12 20:33

    I had the same problem. You should set your webView Height equal as its content. for this do this:
    add this lines to your onCreate method in Activity:

    webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    webView.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
    
                    super.onPageFinished(view, url);
                }
            });
    webView.addJavascriptInterface(this, "MyApp");
    

    and add this method to your activity:

    @JavascriptInterface
        public void resize(final float height) {
            MainActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    webView.setLayoutParams(new LinearLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, (int) (height * getResources().getDisplayMetrics().density)));
                }
            });
        }
    

提交回复
热议问题