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

如何在Cordova Android中检索webview的宽度和高度?,android,cordova,webview,cordova-plugins,Android,Cordova,Webview,Cordova Plugins,我为Android编写了一个Cordova插件 下面是一个简单的初始化函数,它可以恢复mu问题 public class MyPlugin extends CordovaPlugin { CordovaWebView _webView; ViewGroup _root; public void initialize(CordovaInterface cordova, final CordovaWebView webView) { super.initialize(cord

我为Android编写了一个Cordova插件

下面是一个简单的初始化函数,它可以恢复mu问题

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
时,它会显示
-1
。 默认WebView布局是否在Cordova中设置了MATCH_PARENT


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

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

webview.getContentWidth() 及 webview.getContentHeight()

用于获取网页内容的高度和宽度


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

完成页面渲染后,您可以在webview客户端的onPageFinished方法中获得该事件。 你可以用那种方法

webview.getContentWidth() 及 webview.getContentHeight()

用于获取网页内容的高度和宽度


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

我使用了错误的方法。getLayoutParams()不提供实际维度

我的代码在
initialize
execute
功能中工作,具有:

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

我用错了方法。getLayoutParams()不提供实际维度

我的代码在
initialize
execute
功能中工作,具有:

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

Thx,但是在CordovaWebView中没有这样的方法onPageFinished。您可以使用webview.setWebviewClient()为cordova webview设置webview客户端吗;Thx,但是在CordovaWebView中没有这样的方法onPageFinished。您可以使用webview.setWebviewClient()为cordova webview设置webview客户端吗;