我们可以用MXML 和AS创建Element(即一些可视化组件)
下面是用MXML创建:
MXML (
plain)
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<s:Button
left="25"
right="25"
top="25"
bottom="25"
label="Huge s:Button positioned 25px from all sides of s:Application!"/>
</s:Application>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<s:Button
left="25"
right="25"
top="25"
bottom="25"
label="Huge s:Button positioned 25px from all sides of s:Application!"/>
</s:Application>
上面代码中用<s:Button>创建了Button实例 并设置了 left, top, label等一些相关属性 并把它添加到<s:Application>实例中。(即<s:Button>嵌套在<s:Application>中)
下面是用AS创建该元素 :首先我们需要导入包 :import spark.components.Button
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
applicationComplete="onApplicationComplete( event )">
<fx:Script>
<![CDATA[
//import
import mx.events.FlexEvent;
import spark.components.Button;
protected function onApplicationComplete( event:FlexEvent ):void
{
//create new Button instance
var bu:Button = new Button();
bu.left=25;
bu.right=25;
bu.top=25;
bu.bottom=25;
bu.label = "Huge s:Button positioned 25px from all sides of s:Application!";
//add into <s:Application>
this.addElement( bu );//相当于AS里的addChild() ,将其添加进<s:Application>
}
]]>
</fx:Script>
</s:Application>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
applicationComplete="onApplicationComplete( event )">
<fx:Script>
<![CDATA[
//import
import mx.events.FlexEvent;
import spark.components.Button;
protected function onApplicationComplete( event:FlexEvent ):void
{
//create new Button instance
var bu:Button = new Button();
bu.left=25;
bu.right=25;
bu.top=25;
bu.bottom=25;
bu.label = "Huge s:Button positioned 25px from all sides of s:Application!";
//add into <s:Application>
this.addElement( bu );//相当于AS里的addChild() ,将其添加进<s:Application>
}
]]>
</fx:Script>
</s:Application>
上面两种写法运行时是一样的 但是后一个程序是用AS动态的创建的 。