3.20.根据呼出组件设置对话框的尺寸和位置

3.20.1 问题
我们需要生成一个对话框,该对话框具有和呼出它的组件相同尺寸和位置。
3.20.2 解决办法
使用MouseEvent 的target 属性来确定调用该方法的组件的信息,同时,使用mx.geometry.Rectangle 类来确定呼出的组件实际宽高及其在Stage 内的位置。
3.20.3 讨论
为了保证无论应用程序的layout 设定为absolute, horizontal,还是vertical,对话框都会添加到应用程序中并且显示在正确的位置,你需要创建一个可关闭的TabNavigator,该TabNavigator 的所有tab 项都是可关闭的,关闭某个tab 项的时候即从TabNavigator 中移除对应tab 项下的子组件。将对话框的includeInLayout 属性设置为false 以保证应用程序不改变对话框的位置。
+展开
-ActionScript
dialogue = new Dialogue();
mx.core.Application.application.rawChildren.addChild(dialogue);
dialogue.includeInLayout = false;

getBounds 方法返回一个带x 和y 位置的矩形,同时DisplayObject 的宽和高作为传入到方法里面的对象的成员属性。在下面的例子里,Stage 作为传入getBounds 方法的DisplayObject 参数,这样,就会根据整个应用返回组件的位置信息了。
+展开
-XML
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxmlwidth="700"
height="500left="50top="50verticalGap="50">

<mx:Script>
<![CDATA[
import mx.core.Application;
import mx.core.UIComponent;
private var dialogue:Dialogue;
private function placeLabel(event:MouseEvent):void
{
if(dialogue == null)
{
dialogue = new Dialogue();
mx.core.Application.application.rawChildren.addChild(dialogue);
dialogue.includeInLayout = false;
}v
var rect:Rectangle = (event.target as
UIComponent).getBounds(this.stage);
dialogue.x = rect.x;
dialogue.y = rect.y + (event.target as
UIComponent).height;
}

]]>
</mx:Script>
<mx:HBox horizontalGap="50">
<mx:Label text="First label."/>
<mx:Button label="Place Firstclick="placeLabel(event)"/>
</mx:HBox>
<mx:HBox horizontalGap="50">
<mx:Label text="Second label."/>
<mx:Button label="Place Secondclick="placeLabel(event)"/>
</mx:HBox>
<mx:HBox horizontalGap="50">
<mx:Label text="Third label."/>
<mx:Button label="Place Thirdclick="placeLabel(event)"/>
</mx:HBox>
<mx:HBox horizontalGap="50">
<mx:Label text="Fourth label."/>
<mx:Button label="Place Fourthclick="placeLabel(event)"/>
</mx:HBox>
</mx:VBox>

加支付宝好友偷能量挖...


评论(0)网络
阅读(110)喜欢(0)flash/flex/fcs/AIR