手动方向

AIR 2.6 和更高版本

可以使用舞台的 setOrientation() setAspectRatio() 方法控制舞台方向。

设置舞台方向

可以使用 Stage 对象的 setOrientation() 方法在运行时设置舞台方向。使用 StageOrientation 类指定的字符串常量指定所需的方向:

this.stage.setOrientation( StageOrientation.ROTATED_RIGHT );
并不是所有设备和操作系统都支持每个可能的方向。例如,Android 2.2 在纵向标准设备上不支持以编程方式选择向左旋转方向,并且根本不支持向下翻转方向。舞台的 supportedOrientations 属性提供了一个可以传递到 setOrientation() 方法的方向列表:
var orientations:Vector.<String> = this.stage.supportedOrientations; 
for each( var orientation:String in orientations ) 
{ 
    trace( orientation ); 
}

设置舞台高宽比

如果您非常在意舞台的高宽比,则可以将高宽比设置为纵向或横向。可以使用舞台的 setAspectRatio() 方法在 AIR 应用程序描述符中或在运行时设置高宽比:

this.stage.setAspectRatio( StageAspectRatio.LANDSCAPE );

运行时为指定的高宽比选择两个可能的方向之一。这可能与当前设备的方向不匹配。例如,默认的方向选定为首选上下翻转方向(AIR 3.2 及早期版本),适用于滑动式键盘的方向选定为首选反向。

(AIR 3.3 及更高版本) 从 AIR 3.3(SWF 版本 16)开始,您也可以使用 StageAspectRatio.ANY 常数。如果 Stage.autoOrients 设置为 true ,而且您调用了 setAspectRatio(StageAspectRatio.ANY) ,您的应用程序能够将方向重新调整为所有方向(横向-左、横向-右、纵向以及纵向和上下翻转)。同样是 AIR 3.3 的新功能,高宽比保持不变,进一步旋转设备限制为指定方向。

示例:将舞台方向设置为与设备方向相匹配

下面的示例演示了更新舞台方向以匹配当前设备方向的功能。舞台的 deviceOrientation 属性指示设备的物理方向,即使关闭了自动方向也会指示。

function refreshOrientation( theStage:Stage ):void 
{ 
    switch ( theStage.deviceOrientation ) 
    { 
        case StageOrientation.DEFAULT: 
            theStage.setOrientation( StageOrientation.DEFAULT ); 
            break; 
        case StageOrientation.ROTATED_RIGHT: 
            theStage.setOrientation( StageOrientation.ROTATED_LEFT ); 
            break; 
        case StageOrientation.ROTATED_LEFT: 
            theStage.setOrientation( StageOrientation.ROTATED_RIGHT ); 
            break; 
        case StageOrientation.UPSIDE_DOWN: 
            theStage.setOrientation( StageOrientation.UPSIDE_DOWN ); 
            break; 
        default: 
            //No change              
    } 
}

方向更改是异步执行的。可以侦听舞台调度的 orientationChange 事件以检测更改的完成情况。如果某个设备不支持某个方向, setOrientation() 调用将失败且不引发错误。