0

我使用带有 Ionic 2 的适用于 Android 的 Crosswalk 插件,我注意到,在真实设备上运行时,这会产生不一致的结果:

this.platform.ready().then(() => {
        console.log("this.platform.height(): " + this.platform.height() + " / this.platform.width(): " + this.platform.width());
});

有时我得到高度和宽度都> 0:在这种情况下它可以工作。

有时我得到高度和宽度== 0:在这种情况下它不起作用。

我的假设是 Crosswalk 中可能有另一个事件通知它已准备好显示正确的高度和宽度。

我浏览了这个 Crosswalk web api 页面,但没有找到我要找的东西。

4

1 回答 1

0

https://ionicframework.com/docs/v2/api/platform/Platform/

width()
Gets the width of the platform’s viewport using window.innerWidth. Using this method is preferred since the dimension is a cached value, which reduces the chance of multiple and expensive DOM reads.
height()
Gets the height of the platform’s viewport using window.innerHeight. Using this method is preferred since the dimension is a cached value, which reduces the chance of multiple and expensive DOM reads.
  1. 请先尝试使用 window.innerHeight 和 window.innerWidth 来获取设备的高度和宽度。
  2. 尝试

// Add readySource to check if platform ready was used. The resolved value is the readySource, which states which platform ready was used. 
// For example, when Cordova is ready, the resolved ready source is cordova. The default ready source value will be dom.

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';

@Component({...})
export MyApp {
  constructor(platform: Platform) {
    platform.ready().then((readySource) => {
      console.log('Platform ready from', readySource); 
      console.log('Width: ' + platform.width());
      console.log('Height: ' + platform.height());
    });
  }

  1. 删除 Ionic 2 中的 Crosswalk 以检查系统 webview 是否可以得到正确的结果,那么它应该是 Crosswalk 问题而不是 Ionic 问题。

相关主题:https ://forum.ionicframework.com/t/how-to-get-device-width-and-height/28372

于 2016-11-22T08:46:34.677 回答