flex中addPopUp窗口相对浏览器居中
在flex中,如何让addPopUp或者是createPopUp出来的对象相对于浏览器窗口,或者说相对于Application居中?
原本以为用this.parentApplication就可以,后来发现不行,并且很多写法都会报错。
最后看到如下这样的的写法,供大家参考:
PopUpManager.addPopUp( titleWindow, DisplayObject(parentApplication), true);
PopUpManager.centerPopUp(titleWindow);
同时写一个关于弹出窗的例子
首先是使用add方法的
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.controls.Label;
import mx.events.CloseEvent;
import mx.containers.TitleWindow;
import mx.managers.PopUpManager;
private var titleWindow:TitleWindow;
private function init():void {
var label:Label = new Label();
label.text = "欢迎光临Long step";
titleWindow = new TitleWindow();
titleWindow.title = "Custom title";
titleWindow.showCloseButton = true;
titleWindow.width = 240;
titleWindow.height = 180;
titleWindow.addEventListener(CloseEvent.CLOSE, titleWindow_close);
titleWindow.addChild(label);
PopUpManager.addPopUp(titleWindow, DisplayObject(parentApplication), true);
PopUpManager.centerPopUp(titleWindow);
}
private function titleWindow_close(evt:CloseEvent):void {
PopUpManager.removePopUp(titleWindow);
}
]]>
</mx:Script>
<mx:Button label="Launch TitleWindow" click="init()" />
</mx:Application>
下面是使用createPopUp方法的,两种方法的区别是在建立pop窗中对象有先后次序的不同。并且这个里面的例子是相对自身居中。用到的参数是this。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.managers.PopUpManagerChildList;
import mx.controls.Label;
import mx.events.CloseEvent;
import mx.containers.TitleWindow;
import mx.managers.PopUpManager;
private var titleWindow:TitleWindow;
private function init():void {
var label:Label = new Label();
label.text = "欢迎光临Long step";
titleWindow = new TitleWindow();
titleWindow = TitleWindow(PopUpManager.createPopUp(this,TitleWindow,true));
titleWindow.title = "Custom title";
titleWindow.showCloseButton = true;
titleWindow.width = 240;
titleWindow.height = 180;
titleWindow.addChild(label);
titleWindow.addEventListener(CloseEvent.CLOSE, titleWindow_close);
PopUpManager.centerPopUp(titleWindow);
}
private function titleWindow_close(evt:CloseEvent):void {
PopUpManager.removePopUp(titleWindow);
}
]]>
</mx:Script>
<mx:Button label="Launch TitleWindow" click="init()" />
</mx:Application>
1 楼 PursueShadow 2010-12-31 感谢楼主,网上很多例子都不能用,终于按照这种方法实现了。