To lay out a Flex user interface, you insert and position components in a container that has absolute positioning, and then define layout constraints for the components so they adjust automatically when a user resizes the application window. In Flex Builder you can define layout constraints in the design area or add or modify values in the Flex Properties view.
Flex supports constraint-based layouts. There are several approaches to layout: absolute positioning, nested H/VBox layouts, simple constraints, and advanced row/column constraints. The two scenarios outlined in the bullet points are best handled either by nested H/VBoxes or by advanced constraints.
In some situations, you could use nested H/VBoxes or advanced constraints (advanced constraints can only be edited in Source mode):
To create a constraint-based layout, you must use a container that has absolute positioning. The layout="absolute" property overrides the container's layout rule and lets you drag and position components anywhere in the container. This property can be used only with the Application, Canvas, and Panel containers. For Canvas containers, layout="absolute" is the default; for Application and Panel containers, you must set this property explicitly.
Absolute positioning gives you the ability to set layout constraints. For example, if you want a TextInput control to stretch when you make the application window wider, you can anchor the control to the left and right edges of the container so the width of the TextInput control is set by the width of the container.
When you create an MXML application file in Flex Builder, a layout="absolute" property is automatically included in the <mx:Application> tag.
In the following example, all the controls are absolutely positioned in a container either by dragging them or by setting the x and y coordinates in the Flex Properties view:
Also, a number of layout constraints are applied to the controls to ensure that the layout adjusts correctly when the user resizes the application:
The following image shows how the constraints make the controls behave when the user resizes the layout:
The TextInput A and TextInput B controls stretch horizontally as the layout is enlarged. TextArea C control stretches horizontally and vertically. The Button control moves down and to the right.
For more information, see Using Layout Containers in the Adobe Flex 3 Developer Guide.
You may also define a grid of horizontal and vertical constraint regions, ConstraintColumn regions and ConstraintRow regions. Components can be constrained to the edges or centers of these regions similarly to being constrained to the edges or centers of their parent containers. Constraint columns are laid out in the container from left to right, and constraint rows are laid out from top to bottom.
Constraint regions can be defined with fixed pixel dimensions (width or height) or as a percentage of the space in the parent container. The set of constraint columns and rows may have any combination of fixed or percentage dimensions.
Components within a parent container may be constrained to the container or to constraint regions or to any combination of container and region constraints.
For more information about row and column constraints, see the Adobe Flex 3 Developer Guide.
The first step in creating a constraint-based layout is to insert and position components in a container that has absolute positioning.
The component is inserted into your layout.
You use the Flex Properties view to set layout constraints relative to the edges of the parent container. Layout constraints relative to constraint columns and rows can only be set by editing the source code.
If you see a list of properties instead of a form, click the Standard View button in the toolbar.
|
Effect |
Constraints |
|---|---|
|
Maintain the component's position and size |
None |
|
Move the component horizontally |
Left or Right |
|
Move the component vertically |
Top or Bottom |
|
Move the component both horizontally and vertically |
Left + Top or Right + Bottom |
|
Resize the component horizontally |
Left + Right |
|
Resize the component vertically |
Top + Bottom |
|
Resize the component both horizontally and vertically |
Left + Right and Top + Bottom |
|
Center the component horizontally |
Horizontal center |
|
Center the component vertically |
Vertical center |
|
Center the component both horizontally and vertically |
Vertical center + Horizontal center |
For example, you can set the component to maintain its position 90 pixels from the left edge and 60 pixels from the right edge. If the user resizes the application, the component stretches or compresses to maintain these distances from the edges of the application window. Flex Builder expresses these constraints in the MXML code as follows, assuming you set a y property of 160:
<mx:TextInput y="160" left="90" right="60"/>