布局管理器通过3个阶段处理每个可视组件的位置和大小:
两行固定分割
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<mx:Canvas width="100%" height="100%">
<mx:constraintColumns>
<mx:ConstraintColumn id="col1" width="200"/>
<mx:ConstraintColumn id="col2" width="50"/>
</mx:constraintColumns>
<s:Button label="Button1" left="col1:0"/> <!-- 将col1用作ConstraintColumn,向左偏移0 -->
<s:Button label="Button2" left="col2:0"/> <!-- 将col2用作ConstraintColumn,向左偏移0 -->
</mx:Canvas>
</s:Application>
带有左右约束的两列固定分割
<s:Button label="Button1" left="col1:0" right="col1:0"/>
<s:Button label="Button2" left="col2:0" right="col2:0"/>
带有上下约束的两行固定分割
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<mx:Canvas width="100%" height="100%">
<mx:constraintRows>
<mx:ConstraintRow id="row1" height="50%"/>
<mx:ConstraintRow id="row2" height="100"/>
</mx:constraintRows>
<s:Button label="Button1" top="row1:0" bottom="row1:0"/>
<s:Button label="Button2" top="row2:0" bottom="row2:0"/>
</mx:Canvas>
</s:Application>
有偏移的两行加两列分割
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<mx:Canvas width="100%" height="100%">
<mx:constraintColumns>
<mx:ConstraintColumn id="col1" width="100"/>
</mx:constraintColumns>
<mx:constraintRows>
<mx:ConstraintRow id="row1" height="50"/>
<mx:ConstraintRow id="row2" height="50"/>
</mx:constraintRows>
<s:Button label="Button1" left="col1:0" right="col1:0" top="row1:0" bottom="row1:0"/>
<s:Button label="Button2" left="col1:10" right="col1:10" top="row2:0" bottom="row2:0"/>
</mx:Canvas>
</s:Application>
基于内容大小的约束
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<mx:Canvas width="100%" height="100%">
<mx:constraintColumns>
<mx:ConstraintColumn id="col1" />
<mx:ConstraintColumn id="col2" />
</mx:constraintColumns>
<mx:constraintRows>
<mx:ConstraintRow id="row1" height="50"/>
<mx:ConstraintRow id="row2" height="30"/>
</mx:constraintRows>
<s:Button label="Button1" left="col1:0" top="row1:0" width="200"/>
<s:Button label="Button2" left="col1:0" top="row2:0"/>
<s:Button label="Button3" left="col2:0"/>
</mx:Canvas>
</s:Application>
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Group>
<s:layout>
<s:HorizontalLayout/>
</s:layout>
<s:Button label="BTN1"/>
<s:Button label="BTN2"/>
</s:Group>
<s:Group>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Button label="BTN1"/>
<s:Button label="BTN2"/>
</s:Group>
</s:Application>
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:layout>
<s:TileLayout orientation="columns" requestedRowCount="2" />
</s:layout>
<s:Button label="btn1"/>
<s:Button label="btn2"/>
<s:Button label="btn3"/>
</s:Application>
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:layout>
<s:HorizontalLayout />
</s:layout>
<s:Button label="btn1"/>
<mx:Spacer width="50%"/>
<s:Button label="btn2"/>
<mx:Spacer width="50%"/>
<s:Button label="btn3"/>
</s:Application>
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
xmlns:local="*"> <!-- 定义本地命名空间 -->
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
[Bindable]
public var hello:String = "hello!!!";
]]>
</fx:Script>
<local:CustomComponent />
</s:Application>
CustomComponent.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.core.FlexGlobals;
]]>
</fx:Script>
<s:Button label="{FlexGlobals.topLevelApplication.hello}"/>
</s:Group>
CoolSkin.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:SparkSkin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:fb="http://ns.adobe.com/flashbuilder/2009" alpha.disabled="0.5">
<!--- 定义支持状态 -->
<s:states>
<s:State name="normal" />
<s:State name="disabled" />
</s:states>
<!--- 背景中使用的矩形 -->
<s:Rect height="100%" width="100%">
<s:fill>
<s:LinearGradient>
<s:entries>
<s:GradientEntry color="#92A1B9"/>
</s:entries>
</s:LinearGradient>
</s:fill>
</s:Rect>
<s:Group id="contentGroup" left="5" right="5" top="5" bottom="5" >
<s:layout>
<s:BasicLayout/>
</s:layout>
</s:Group>
</s:SparkSkin>
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
xmlns:local="*"> <!-- 定义本地命名空间 -->
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:SkinnableContainer skinClass="CoolSkin"><!--- 指定皮肤 -->
<s:layout>
<s:HorizontalLayout/>
</s:layout>
<s:Button label="button 1"/>
<s:Button label="button 2"/>
</s:SkinnableContainer>
</s:Application>
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
xmlns:local="*"> <!-- 定义本地命名空间 -->
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:Panel title="My Panel">
<s:HGroup top="5" bottom="5" left="5" right="5">
<s:Button label="button1"/>
<s:Button label="button2"/>
</s:HGroup>
</s:Panel>
</s:Application>
DataGroup和SkinnableDataContainer
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
xmlns:local="*"> <!-- 定义本地命名空间 -->
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var $data:ArrayCollection = new ArrayCollection(["item1", "item2", "item3"]);
]]>
</fx:Script>
<s:DataGroup dataProvider="{$data}"
itemRenderer="spark.skins.spark.DefaultItemRenderer">
<s:layout>
<s:HorizontalLayout/>
</s:layout>
</s:DataGroup>
</s:Application>
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
xmlns:local="*"> <!-- 定义本地命名空间 -->
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
<s:ArrayCollection id="$data">
<s:Button label="button 1"/>
<s:Button label="button 2"/>
<s:Button label="button 3"/>
</s:ArrayCollection>
</fx:Declarations>
<s:DataGroup dataProvider="{$data}"
itemRenderer="spark.skins.spark.DefaultComplexItemRenderer">
<s:layout>
<s:HorizontalLayout/>
</s:layout>
</s:DataGroup>
</s:Application>
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
xmlns:local="*"> <!-- 定义本地命名空间 -->
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:Panel title="My Panel">
<s:HGroup top="5" bottom="5" left="5" right="5">
<s:Button label="button1"/>
<s:Button label="button2"/>
</s:HGroup>
</s:Panel>
</s:Application>
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
xmlns:local="*"> <!-- 定义本地命名空间 -->
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:Panel title="Report Dashboard" verticalCenter="0" horizontalCenter="0">
<mx:DividedBox direction="horizontal" width="100%">
<s:VGroup height="100%">
<mx:Text text="Catogries" fontWeight="bold"/>
<s:Button label="Finance" width="100%"/>
<s:Button label="Operations" width="100%"/>
</s:VGroup>
<mx:VDividedBox width="50%" height="100%">
<s:VGroup width="100%">
<mx:Text text="Finance Reports" fontWeight="bold"/>
<mx:Text text="2008 Q1 sales" />
<mx:Text text="2008 Q2 sales" />
</s:VGroup>
<s:VGroup width="100%">
<mx:Text text="Finance Reports2" fontWeight="bold"/>
<mx:Text text="2008 Q1 sales" />
<mx:Text text="2008 Q2 sales" />
</s:VGroup>
</mx:VDividedBox>
</mx:DividedBox>
</s:Panel>
</s:Application>
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
xmlns:local="*"> <!-- 定义本地命名空间 -->
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<mx:Form>
<mx:FormHeading label="Contact Info"/>
<mx:FormItem label="First Name">
<s:TextInput/>
</mx:FormItem>
<mx:FormItem label="Last Name">
<s:TextInput/>
</mx:FormItem>
</mx:Form>
</s:Application>
<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
xmlns:local="*"> <!-- 定义本地命名空间 -->
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<mx:Grid>
<mx:GridRow>
<mx:GridItem>
<s:Button label="Rewind"/>
</mx:GridItem>
<mx:GridItem>
<s:Button label="Play"/>
</mx:GridItem>
<mx:GridItem>
<s:Button label="Forward"/>
</mx:GridItem>
</mx:GridRow>
<mx:GridRow>
<mx:GridItem colSpan="3">
<s:Button label="STOP" width="100%"/>
</mx:GridItem>
</mx:GridRow>
</mx:Grid>
</s:Application>