在Android或iOS平台的大多数项目中,可能只针对横屏或者竖屏来做界面的适配。
在air中,可以用到 StageOrientationEvent.ORIENTATION_CHANGING 这个事件来让程序只接受横屏或竖屏的旋转操作,让程序始终保持横屏或竖屏。
以横屏为例:
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageOrientation;
import flash.display.StageScaleMode;
import flash.events.StageOrientationEvent;
import flash.text.TextField;
import flash.text.TextFormat;
public class Test extends Sprite
{
private var _label:TextField;
public function Test()
{
super();
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
_label = new TextField();
_label.width = 600;
_label.height = 400;
_label.border = true;
_label.defaultTextFormat = new TextFormat(null, 20, 0);
this.addChild(_label);
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChangingHandler);
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, orientationChangeHandler);
_label.text = "Default Orientation: " + stage.orientation;
if(stage.orientation != StageOrientation.ROTATED_LEFT && stage.orientation != StageOrientation.ROTATED_RIGHT) {
stage.setOrientation(StageOrientation.ROTATED_RIGHT);
}
}
private function orientationChangingHandler(event:StageOrientationEvent):void
{
if(event.afterOrientation != StageOrientation.ROTATED_LEFT && event.afterOrientation != StageOrientation.ROTATED_RIGHT) {
event.preventDefault();//阻止设备旋转到竖屏
_label.appendText("\n不允许旋转到: " + event.afterOrientation);
}
}
private function orientationChangeHandler(event:StageOrientationEvent):void
{
_label.appendText("\n设备已选择至: " + event.afterOrientation);
}
//
}
}
app.xml: <initialWindow><autoOrients>true</autoOrients></initialWindow>
或: stage.autoOrients = true;