如何在Cordova Android中检索webview的宽度和高度?

时间:2016-07-02 11:27:38

标签: android cordova webview cordova-plugins

我为Android编写了一个Cordova插件。

这是一个恢复mu问题的简单初始化函数

canny

请注意,在记录public class MyPlugin extends CordovaPlugin { CordovaWebView _webView; ViewGroup _root; public void initialize(CordovaInterface cordova, final CordovaWebView webView) { super.initialize(cordova, webView); _webView = webView; _root = (ViewGroup) _webView.getView().getParent(); Log.d(TAG, Integer.toString(root.getLayoutParams().width)); // -1 Log.d(TAG, Integer.toString(webView.getView().getLayoutParams().width)); //-1 too } public boolean execute(final String action, final CordovaArgs args, final CallbackContext _callbackContext) throws JSONException { Log.d(TAG, Integer.toString(_root.getLayoutParams().width)); // -1 Log.d(TAG, Integer.toString(_webView.getView().getLayoutParams().width)); // -1 ... } } 时,它会显示FrameLayout.LayoutParams.MATCH_PARENT。 是否在Cordova中使用MATCH_PARENT设置了默认的WebView布局?

如何使用/ heigh检索WebView或WebView的父级?

2 个答案:

答案 0 :(得分:0)

页面渲染完成后,您可以在WebView客户端的onPageFinished方法中获取该事件。 在该方法中,您可以使用

webview.getContentWidth() 和 webview.getContentHeight()

获取内容的网页高度和宽度。

注意: - 此方法适用于android stock webview。希望它适用于cordova webview。

答案 1 :(得分:0)

我使用了错误的方法。 getLayoutParams()不给出实际尺寸。

我的代码在initializeexecute函数中都可以使用:

Log.d(TAG, Integer.toString(_webView.getView().getWidth()));
Log.d(TAG, Integer.toString(_webView.getView().getHeight()));