6.05.2008

Layout Fundamentals

Flex provides several methods for configuring the layout of UI components. It is important to understand the details of the layout process in order to successfully position UI components.

The layout of components in a container is updated whenever an event occurs that might affect component layout, such as resizing the container, adding or removing components or changing the appearance of components. If is also possible to force a layout to occur by calling UIComponent.validateNow().

Flex components have a default size but may be customized by setting an explicit size (in pixels) or a size as a percent of the available space in its parent container. Pixel-based size and percent-based size are mutually exclusive: the last one set will override the first one set. Component size may also be adjusted by the layout manager when using a constraint-based layout.

Minimum and maximum sizes may be set on components, which will be honored unless an explicit size is set outside the minimum/maximum boundaries.

When a container updates the layout of its components it uses this process:

1. Determine measured sizes
- starting with innermost component, proceeding to outermost component
- return the explicit size if set on the component
- otherwise return the default size

2. Determine percent sizes and adjust position
- starting with outermost component, proceeding to innermost component
- if a percent size is set adjust the size of component accordingly
- position the component according to the layout manager rules for the container

Sometimes it is useful to add borders around components in order to visualize layout problems. Set properties borderStyle="solid" borderColor="0x00FF00" on components to achieve this.

mx:Spacer is an invisible component provided to help arrange components by taking up space. You typically will set the width or height of the spacer to a percent value.

Scrollbars will appear on a container if its children require more space than is available. Since the size of the scrollbars is not calculated during the layout process it is possible that the scrollbars will overlap components in the container. It is possible to adjust the scrollbar behavior by setting clipContent="false" on the container.

The Flex SDK includes several types of containers which are listed here, grouped by layout type.

Navigator Containers
Accordian, TabNavigator
One child component is visible at a time.

Tile Layout
Tile
A grid with equal-sized cells. Items wrap around to new columns or rows (like the Java FlowLayout).

Grid Layout
Grid
A grid arrangement with unequal-sized cells and support for column/row spans (like the Java GridBagLayout).

CanvasLayout
Canvas, Panel with layout="absolute", Application with layout="absolute", Module with layout="absolute"
Children may be positioned by statically setting x/y/width/height or by anchoring children to the container or to constraint rows/columns.

BoxLayout
Box, HBox, VBox, ControlBar, DividedBox, HDividedBox, VDividedBox, NavBar, ButtonBar, ToggleButtonBar, TabBar, LinkBar, Form, FormItem, Panel with layout="vertical" or "horizontal", Application with layout="vertical" or "horizontal", Module with layout="vertical" or "horizontal"
Children arranged in a single row or column (like a Java BoxLayout).

The Adobe Flex 3 Developer guide provides a thorough discussion of layout in Chapter 8: Sizing and Positioning Components.

No comments: