Layout classes may be used with any Spark container to configure the way Flex components appear on the screen. Your finished application will appear as shown in Figure 1.

Figure 1. Take stock of your tasks in this exercise.
In this exercise, you will learn how to:
In order to complete this tutorial, you need the following software and files:
In this section, you will create and configure a Flex application with an absolute layout.
In the Package Explorer view, expand the components folder to reveal the EmployeeDisplay.mxml component (see Figure 2).

Figure 2. See the EmployeeDisplay.mxml component.
Below the UI components comment, look for the Label control and the four instances of the
EmployeeDisplay MXML component (see Figure 3).

Figure 3. View the custom component instances.
Note the extra employees comment and the component
instances within. You will use these component instances later in this exercise.
Run the application.
Note that the components are displayed diagonally from top-left to bottom-right (see Figure 4).

Figure 4. Run the application.
In this section you will change the layout of the application to see the differences between layouts.
Below the Properties of
parent comment, create a set of layout tags.
<!-- Properties of the parent
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<s:layout>
</s:layout>
Within the layout property, change the layout class to the BasicLayout class.
<s:layout>
<s:BasicLayout/>
</s:layout>
Run the application.
The application should look identical to Figure 4 (see Figure 5).

Figure 5. Run the application and see there is no change.
Within the Layout tags, change the layout from BasicLayout to TileLayout.
<s:layout>
<s:TileLayout/>
</s:layout>
Save the file and run the application.
You should see that the fields are now arranged using the TileLayout (see Figure 6).

Figure 6. Run the application using the TileLayout class.
Change the layout from TileLayout to HorizontalLayout.
<s:layout>
<s:HorizontalLayout/>
</s:layout>
Save the file and run the application.
You should see that the x and y properties of the Label controls are ignored as shown in Figure 7.

Figure 7. Run the application using the HorizontalLayout class.
Change the layout to VerticalLayout.
<s:layout>
<s:VerticalLayout/>
</s:layout>
You should see the components aligned vertically as shown in Figure 8.

Figure 8. Run the application to see the components aligned vertically.
In this section you will use multiple Flex layout containers to change the display of the sample application.
In Source mode, nest the
instances of the EmployeeDisplay MXML component within a Group container.
<s:Group>
<components:EmployeeDisplay empImage="images/bvanbrocklin.jpg"
empName="Brad VanBrocklin"
empTitle="Instructional Designer" y="46"/>
<components:EmployeeDisplay empImage="images/abrilliam.jpg"
empName="Andrew Brilliam"
empTitle="Senior Hardware Engineer"
x="138" y="157"/>
<components:EmployeeDisplay empImage="images/akotter.jpg"
empName="Annette Kotter"
empTitle="Vice President of Market Development"
x="267" y="267"/>
<components:EmployeeDisplay empImage="images/rcrawley.jpg"
empName="Rosco Crawley"
empTitle="Regional Manager"
x="413" y="378"/>
</s:Group>
To the Group container, add the layout tags with a nested VerticalLayout class
tag.
<s:Group>
<s:layout>
<s:VerticalLayout/>
</s:layout>
...
Save the file and run the application.
You should see the employes are displayed vertically as shown in Figure 9.

Figure 9. See the EmployeeDisplay components are ordered vertically.
Change the Group container to a Panel container.
<s:Panel>
<s:layout>
<s:VerticalLayout/>
</s:layout>
...
</s:Panel>
Save the file and run the application.
You should see the Panel container displays the employees like the Group container, but has the panel look and feel (see Figure 10).

Figure 10. Display the employees using a Panel container.
Change the Panel container to a VGroup container.
<s:VGroup>
<s:layout>
<s:VerticalLayout/>
</s:layout>
...
</s:VGroup>
From the VGroup container, remove the layout tags.
<s:VGroup> ... </s:VGroup>
Save the file and run the application.
You should see the employees are ordered the same way as they
were with the Panel and Group containers without using the VerticalLayout class (see Figure 11).

Figure 11. See the employees are displayed vertically.
Change the VGroup container to an HGroup container.
<s:HGroup> ... </s:HGroup>
You should see the employees are ordered horizontally within the application as shown in Figure 12.

Figure 12. Display the employees using an HGroup container.
In this section you will use multiple layout tags to
organize the application content.
Below the Properties of
the parent comment, change the layout property to use the TileLayout class.
<!-- Properties of the parent
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<s:layout>
<s:TileLayout />
</s:layout>
HGroup container tags from around the EmployeeDisplay component instances.Run the application.
You should see the title and eight EmployeeDisplay instances
arranged as shown in Figure 13.

Figure 13. View the application using a TileLayout class.
Revert the application's
layout class to VerticalLayout.
<s:layout>
<s:VerticalLayout/>
</s:layout>
Nest all eight instances of
the EmployeeDisplay component within a Group container. Ensure
the Label control is not included in the group.
<s:Label x="9" y="9" text="Employee Directory" ... />
<s:Group>
<component:EmployeeDisplay ... />
<component:EmployeeDisplay ... />
<component:EmployeeDisplay ... />
<component:EmployeeDisplay ... />
<component:EmployeeDisplay ... />
<component:EmployeeDisplay ... />
<component:EmployeeDisplay ... />
<component:EmployeeDisplay ... />
</s:Group>
Within the new Group container, use the layout tags to apply the TileLayout class to the container.
<s:Group>
<s:layout>
<s:TileLayout />
</s:layout>
...
<s:Group>
You should see the Group container with
the TileLayout class is placed below the Label control as shown in Figure 14.

Figure 14. Display the group of employees using the TileLayout class.
In this section you will organize the controls within the EmployeeDisplay component using a layout class and properties.
From the Image and Label controls, delete the x and y properties and their
values.
<mx:Image source="{empImage}"/>
<s:Label text="{empName}"
fontWeight="bold"
textAlign="center"
width="120"/>
<s:Label width="120"
text="{empTitle}"
textAlign="center" />
Save the file and run the application.
You should see the controls within the EmployeeDisplay component instances are displayed on top of one another (see Figure 15).

Figure 15. View the application without a defined layout for the EmployeeDisplay component.
Properties of the parent comment. Below the comment, create
a set of layout tags.
<!-- Properties of the parent
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<s:layout>
</s:layout>
Within the layout tags,
add a VerticalLayout class.
<s:layout> <s:VerticalLayout/> </s:layout>
Save the file and run the application.
You should see that the controls are arranged vertically, but
are not centered as shown in Figure 16. The text controls may appear centered,
but that is only because the text controls themselves are set with textAlign="center" properties.

Figure 16. View the application with the VerticalLayout class defined.
To the VerticalLayout instance, add the horizontalAlign property and set its value to center.
<s:VerticalLayout horizontalAlign="center"/>
You should see the controls are now centered within each EmployeeDisplay instance, as shown in Figure 17.

Figure 17. View the application with the EmployeeDisplay custom component UI controls horizontally centered.
In this exercise you learned how to use Flex layout containers and apply multiple layouts to an application.
This content was authored by Adobe Systems, Inc.