【问题标题】:How to retrieve width and heigh of the webview in Cordova Android?如何在 Cordova Android 中检索 webview 的宽度和高度?
【发布时间】:2016-07-02 11:27:38
【问题描述】:

我为 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 的父级?

【问题讨论】:

    标签: android cordova webview cordova-plugins


    【解决方案1】:

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

    webview.getContentWidth() 和 webview.getContentHeight()

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

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

    【讨论】:

    • 谢谢,但是CordovaWebView中没有onPageFinished这样的方法。
    • 你能否使用 webview.setWebviewClient() 为你的 Cordova webview 设置 webview 客户端;
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多