2 May 2011
You can use layout classes with any Spark container to configure the way Flex components appear on the screen. Figure 1 shows the finished application.
In this section, you will create and configure a Flex application with an absolute layout.
UI components comment, look for the Label control and the three instances of the EmployeeDisplay MXML component.<components:EmployeeDisplay empImage="images/aparker.jpg" empName="Athena Parker"
empTitle="Instructional Designer" y="86"/>
<components:EmployeeDisplay empImage="images/stucker.jpg" empName="Saul Tucker"
empTitle="Senior Hardware Engineer" x="138" y="197"/>
<components:EmployeeDisplay empImage="images/sang.jpg" empName="Samuel Ang"
empTitle="Vice President of Market Development" x="267" y="307"/>
Note the extra employees comment and the component instances within. You will use these component instances later in this exercise.
Note that the components are displayed diagonally from top left to bottom right (see Figure 3).
<!-- Properties of the parent ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<s:layout>
</s:layout>
layout property, change the layout class to the BasicLayout class.<s:layout>
<s:BasicLayout/>
</s:layout>
The application should look identical to Figure 3.
layout tags, change the layout from BasicLayout to TileLayout. <s:layout>
<s:TileLayout/>
</s:layout>
You should see that all the controls, including the Label control, are now arranged using the TileLayout. You should also see that the Employee Portal: Employee Directory label resizes to the height of the components. (see Figure 6).
TileLayout to HorizontalLayout.<s:layout>
<s:HorizontalLayout/>
</s:layout>
Note that the x and y properties of the controls are ignored (see Figure 5).
VerticalLayout.<s:layout>
<s:VerticalLayout/>
</s:layout>
You should see the components aligned vertically. Notice that the Label control and the components are flush with the left edge of the browser(see Figure 6).
VerticalLayout tag, add a paddingLeft property with a value of 15 and a paddingTop property with a value of 15.<s:layout>
<s:VerticalLayout paddingLeft="15" paddingTop="15"/>
</s:layout>
Note the new position of the Label control and the EmployeeDisplay components in the browser.
<s:Group>
<components:EmployeeDisplay empImage="images/aparker.jpg"
empName="Athena Parker"
empTitle="Instructional Designer" y="86"/>
<components:EmployeeDisplay empImage="images/stucker.jpg"
empName="Saul Tucker"
empTitle="Senior Hardware Engineer"
x="138" y="197"/>
<components:EmployeeDisplay empImage="images/sang.jpg"
empName="Samuel Ang"
empTitle="Vice President of Market Development"
x="267" y="307"/>
</s:Group>
Group container, add the layout tags with a nested VerticalLayout tag.<s:Group>
<s:layout>
<s:VerticalLayout/>
</s:layout>
...
</s:Group>
The employee thumbnails are still displayed vertically (see Figure 7).
Group container to a Panel container.<s:Panel>
<s:layout>
<s:VerticalLayout/>
</s:layout>
...
</s:Panel>
The Panel container displays the employees like the Group container, but has the panel look and feel (see Figure 8).
Panel container to a VGroup container.<s:VGroup>
<s:layout>
<s:VerticalLayout/>
</s:layout>
...
</s:VGroup>
VGroup container, remove the layout tags.<s:VGroup>
...
</s:VGroup>
The employees are now ordered the same way as they were with the Panel and Group containers without using the VerticalLayout class (see Figure 9).
VGroup container to an HGroup container.<s:HGroup>
...
</s:HGroup>
The employees are ordered horizontally within the application (see Figure 10).
<!-- Properties of the parent ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<s:layout>
<s:TileLayout paddingLeft="15" paddingTop="15"/>
</s:layout>
HGroup container tags from around the EmployeeDisplay component instances.extra employees comment so the other component instances will be added to the application.You should see the title and the EmployeeDisplay instances arranged as shown in Figure 11.
VerticalLayout.<s:layout>
<s:VerticalLayout paddingLeft="15" paddingTop="15"/>
</s:layout>
EmployeeDisplay component within a Group container. Ensure the Label control is not included in the group.<s:Label x="15" y="15" width="520" height="40" text="Employee Portal: Employee Directory"
styleName="titleHeader"/>
<s:Group>
<component:EmployeeDisplay ... />
<component:EmployeeDisplay ... />
<component:EmployeeDisplay ... />
<component:EmployeeDisplay ... />
<component:EmployeeDisplay ... />
<component:EmployeeDisplay ... />
<component:EmployeeDisplay ... />
<component:EmployeeDisplay ... />
</s:Group>
Group container, use the layout tags to apply the TileLayout class to the container.<s:Group>
<s:layout>
<s:TileLayout />
</s:layout>
...
<s:Group>
The Group container with the TileLayout class is placed below the Label control. Note that the spacing between the Label controls and the EmployeeDisplay components can be improved (see Figure 12).
paddingTop property with a value of 15.<s:layout>
<s:TileLayout paddingTop="15"/>
</s:layout>
Note that there is more space between the Label control and the other components (see Figure 13).
In this section you will organize the controls within the EmployeeDisplay component using a layout class and properties.
EmployeeDisplay.mxml file in the components directory.Bitmap Image and Label controls, delete the x and y properties and their values.<s:BitmapImage source="{empImage}" width="74" height="74"/>
<s:Label text="{empName}"
fontWeight="bold"
textAlign="center"
width="120"/>
<s:Label text="{empTitle}" textAlign="center" width="120"/>
You should see the controls within the EmployeeDisplay component instances are displayed on top of one another (see Figure 14).
Properties of the parent comment.layout tags.<!-- Properties of the parent ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<s:layout>
</s:layout>
layout tags, add a VerticalLayout class.<s:layout>
<s:VerticalLayout/>
</s:layout>
Note that the controls are arranged vertically, but are not centered (see Figure 15).
VerticalLayout instance, add the horizontalAlign property and set its value to center.<s:VerticalLayout horizontalAlign="center"/>
The BitmapImage controls are now centered within each EmployeeDisplay instance (see Figure 16).
In this section, you will use the Spark BorderContainer class to create a border around the employee images. The BorderContainer container can be used in place of a skin to create a border and background for a component.
Group container that nests the EmployeeDisplay component blocks with a BorderContainer component.<s:BorderContainer>
<s:Group>
<s:layout>
<s:TileLayout/>
</s:layout>
<components:EmployeeDisplay ... />
...
</s:Group>
</s:BorderContainer>
BorderContainer tag, add the borderColor property with a value of #808080,a width property with a value of 500, a cornerRadius property with a value of 5 and a borderWeight property with a value of 2:<s:BorderContainer borderColor="#808080" width="500"
cornerRadius="5" borderWeight="2">
The EmployeeDisplay components are now surrounded with a border (see Figure 17).
In this section, you will use the Scroller component to add scrollbars to the application.
Group container that nests the EmployeeDisplay component blocks, add an id property with a value of employeeGroup, a height property of 260 and a width property of 500.<s:BorderContainer borderColor="#808080" width="500"
cornerRadius="5"
borderWeight="2">
<s:Group id="employeeGroup" height="260" width="500">
<s:layout>
<s:TileLayout/>
</s:layout>
...
</s:Group>
BorderContainer block, between the closing BorderContainer tag and the closing Group container tag, create a Scroller component. </s:Group>
<s:Scroller/>
</s:BorderContainer>
Scroller component, add the viewport property and bind it to the value of the Group container's id property, employeeGroup. </s:Group>
<s:Scroller viewport="{employeeGroup}"/>
</s:BorderContainer>
The application now has a vertical scrollbar that allows you to scroll the employee list (see Figure 18).
In this exercise you learned how to use Flex layout containers and apply multiple layouts to an application. In the next exercise you will use Flash Builder to create an MXML class and reuse the class to display employee information.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.