In many cases it is possible to layout components using one of the simple layout approaches implemented in the HBox, VBox, Tile or Form containers. When a more advanced layout is needed the obvious choices are Grid and Canvas (using enhanced constraints).
The Grid provides the ability to position components in rows and columns, setting the size of the rows and columns to fill extra space as desired and setting rowSpan and columnSpan properties on the items to create complex arrangements.
The Canvas, when set to layout="absolute" supports a new approach called "Enhanced Constraints" through the use of constraint columns and constraint rows. This allows components to be anchored to the container as well as to virtual rows and columns which can be sized explicitely, by percent or according to the components within them.
If you do some research (specifically: http://bugs.adobe.com/jira/browse/SDK-12941) you will find that Grid was initially deprecated in the Flex 3 beta 2 release, but that decision was reversed before the production release of Flex 3. In the bug comments it is noted that there are no current plans to deprecate or remove Grid but it is obvious that the Flex team believes that Enhanced Constraints represents the future direction of Flex layout.
After some experimentation it appears that the Enhanced Constraints feature is not fully production ready. In several cases it was observed that components did not fill extra space or line up as specified in the constraints. It is possible that my tests were not making proper use of the new functionality, only a careful walk through the CanvasLayout.as source will verify the source of the problem. I have filed one bug at this point and will add more later http://bugs.adobe.com/jira/browse/SDK-15746.
At this point it makes sense to favor Grid for advanced layouts but in time the Enhanced Constraints feature will probably overtake Grid.
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.
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.
Coordinate Systems
Flex supports three different coordinate systems:
- Global: 0,0 is the upper left corner of the stage
- Local: 0,0 is the upper left corner of the component (including margins and borders)
- Content: 0,0 is the upper left corner of the component's content (not including margins and borders), even if the content is currently hidden due to scrolling
- contentToGlobal()
- contentToLocal()
- globalToContent()
- globalToLocal()
- localToContent()
- localToGlobal()
Container Notes
- Default buttons are defined per container, using the defaultButton property
- Setting the enabled property to false for a container disables the entire container (unlike the behavior in Swing)
Subscribe to:
Posts (Atom)