我为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的父级?
答案 0 :(得分:0)
页面渲染完成后,您可以在WebView客户端的onPageFinished方法中获取该事件。 在该方法中,您可以使用
webview.getContentWidth() 和 webview.getContentHeight()
获取内容的网页高度和宽度。
注意: - 此方法适用于android stock webview。希望它适用于cordova webview。
答案 1 :(得分:0)
我使用了错误的方法。 getLayoutParams()不给出实际尺寸。
我的代码在initialize
和execute
函数中都可以使用:
Log.d(TAG, Integer.toString(_webView.getView().getWidth()));
Log.d(TAG, Integer.toString(_webView.getView().getHeight()));