Flex中如何使用addChild()和removeChild()函数动态添加或删除Accordion容器中项目的例子
By Minidxer | October 5, 2008
接下来的例子演示了Flex中如何使用addChild()和removeChild()函数,动态添加或删除Accordion容器中项目。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white">
- <mx:Script>
- <![CDATA[
- import mx.containers.VBox;
- private const MAX_CHILDREN:uint = 5;
- private function accordion_addChild():void {
- if (accordion.numChildren < MAX_CHILDREN) {
- var vbox:VBox = new VBox();
- vbox.label = "child " + accordion.numChildren;
- vbox.percentWidth = 100;
- vbox.percentHeight = 100;
- var randColor:uint = Math.random() * 0xFFFFFF;
- vbox.setStyle("backgroundColor", randColor);
- accordion.addChild(vbox);
- }
- }
- private function accordion_deleteChild():void {
- if (accordion.selectedChild) {
- accordion.removeChild(accordion.selectedChild);
- }
- }
- ]]>
- </mx:Script>
- <mx:ApplicationControlBar dock="true">
- <mx:Button label="Add child"
- click="accordion_addChild();" />
- <mx:Button label="Delete child"
- click="accordion_deleteChild();" />
- </mx:ApplicationControlBar>
- <mx:Accordion id="accordion" width="240" height="160" />
- </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子
Topics:
Accordion |
2 Comments » |
Tags: Accordion, AddChild, removeChild()
有没有动态加载视频的例子?
在删除时好像需要强制类型转换一下
accordion.removeChild((DisplayObject)(accordion.selectedChild));