0

我想根据各种平台动态更改图像的高度和宽度。

我已经检查了 appcelerator 的 platform.height 和 platform.width 的文档。但我似乎无法理解它是如何完成的。

4

1 回答 1

2

您可以使用此跨平台代码以 dp 获取 Android/iOS 设备的宽度和高度。

合金.js

var df = Ti.Platform.displayCaps.logicalDensityFactor;
var w = OS_IOS ? Ti.Platform.displayCaps.platformWidth : Ti.Platform.displayCaps.platformWidth / df;
var h = OS_IOS ? Ti.Platform.displayCaps.platformHeight : Ti.Platform.displayCaps.platformHeight / df;

Alloy.Globals.deviceWidth = Math.min(w, h);
Alloy.Globals.deviceHeight = Math.max(w, h);

现在您可以在任何您想要的文件中使用这些全局变量,如下所示:

"#someLabel": {
   width : Alloy.Globals.deviceWidth,
   height : 100
}
于 2017-08-09T12:03:02.913 回答