如何更改WebView大小(window.innerWidth; window.innerHeight)?

时间:2018-11-14 08:22:08

标签: android android-webview

如何更改Webview(window.innerWidth; window.innerHeight)? WebSettings似乎没有做到这一点的方法。

编辑: 我在这里添加解决方案以简化操作:

webView.setLayoutParams(new FrameLayout.LayoutParams(
                200, // width
                200 // height
        ));

1 个答案:

答案 0 :(得分:1)

来源:How to resize a android webview after adding data in it

private void setupWebView() {
    webView.getSettings().setJavaScriptEnabled(true);
    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");
}
@JavascriptInterface
public void resize(final float height) {
    MyActivity.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            webView.setLayoutParams(new LinearLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, (int) (height * getResources().getDisplayMetrics().density)));
        }
    });
}