flex addChild 的一个小细节
关键字: flex, addchild
如果addChild的参数是别的容器的child, 那么这个child就会被"移动"(即先从就容器中removeChild掉,然后在addChild到新容器中,没必要自己去removeChild)到新的容器里
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal"
- width="800"
- height="600"
- creationComplete="init()">
- <mx:Script>
- <![CDATA[
- import mx.controls.Label;
- private var lab:Label = new Label();
- private function init():void
- {
- lab.text = "I am a label";
- c1.addChild(lab);
- }
- private function onClick(event:MouseEvent):void
- {
- if(event.currentTarget == c1 &&lab.parent != c1)
- {
- c1.addChild(lab);
- }
- if(event.currentTarget == c2 &&lab.parent != c2)
- {
- c2.addChild(lab);
- }
- }
- ]]>
- </mx:Script>
- <mx:Canvas id="c1" width="50%" height="100%" backgroundColor="blue" click="onClick(event)"/>
- <mx:Canvas id="c2" width="50%" height="100%" backgroundColor="red" click="onClick(event)"/>
- </mx:Application>