在实际开发中主界面总是要充满全屏,但是在程序中如何获取当前窗口的实际大小呢 ??
问题描述:
主程序:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
xmlns:local="*" width="100%" height="100%" >
<local:subView width="100%" height="100%" >
</local:subView>
</mx:Application>
组件 subView:
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" >
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public function init():void{
Alert.show(String(this.width));//获取实际大小
Alert.show(String(Application.application.width));//主场景实际大小
}
]]>
</mx:Script>
</mx:Canvas>
运行结果总是 显示 宽度 高度为零!!
解决办法:
主程序:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
xmlns:local="*" width="100%" height="100%" initialize="mainInit()" >
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
private function mainInit():void{
//子组建创建完成后执行init2()
subCanvas.addEventListener(FlexEvent.CREATION_COMPLETE,subCanvas.init2);
}
]]>
</mx:Script>
<local:subView width="100%" height="100%" id="subCanvas">
</local:subView>
</mx:Application>
组件 subView:
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" >
<mx:Script>
<![CDATA[
import mx.core.Application;
import mx.controls.Alert;
public function init():void{
Alert.show(String(this.width));
Alert.show(String(Application.application.width));
}
public function init2(evt:Event):void{
Alert.show(String(this.width));
Alert.show(String(Application.application.width));
}
]]>
</mx:Script>
</mx:Canvas>
终于看到实际大小了,这样做比较麻烦,不知有没有更好的解决办法。
参考文章:
UIComponent的生命周期(life cycle)
地址 : http://www.5uflash.com/Flex-AIR/Flexziliao/1556.html