|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
CF & Flex User Interface Layout in Adobe Flex 2.0
Creating a user interface is just like making a floor plan
By: Peter Ent
Aug. 11, 2006 08:00 PM
Creating a user interface is just like making a floor plan. You have your "room," which is the screen, and you have the "furniture," which is your user interface elements. This article describes many of the ways to position those elements on the screen using XML tags (which we call MXML.)
There's a Layout Manager in the Flex architecture. This is the entity that is responsible for carrying out the layout instructions. Once the components are created, the Layout Manager asks every container for its size. Each container in turn asks each child for its size. This continues until the Layout Manager has a pretty good idea of how big everything is. Then it carries out the placement instructions as best it can.
Layout <mx:Button label="Push Me" x="10" y="30" /> Simple enough. You can use this technique to position all of your components. That's all well and good, but now suppose you want a layout like this: Besides position, most components have width and height properties. To start, you could create your layout like this:
<mx:Text text="My Title Goes Here" x="35" y="10" /> Now comes the question: How do I make the DataGrid take up the entire width of the application space and how do I make the DataGrid use up the rest of the height? In other words, you have no idea how big the user will make their browser. It could be 200x250. Or it could be 1024x768. It would be nice if the components could automatically expand to fill the space. Actually, that's easy. Not only can you specify the width and height of a component in pixels, you can also specify it as a percentage of the container in which they are placed. This is an important fact and we'll come back to it shortly. <mx:DataGrid x="0" y="50" width="100%" height="100%" /> Using 100% for the width and height makes the DataGrid use up the space in those directions. When I first saw this I expected that I would have to know the height of the Text control, or the percentage of space that the Text control was using. Not necessary. The Flex Layout Manager figures it out. It knows you want the top of the DataGrid to be positioned at 50 pixels. Having a height of 100% means it takes up the rest of the vertical space. When you run this Flex application you can resize the browser and the DataGrid will adjust its size. But what about that title Text? Suppose you'd like it to be centered above the DataGrid and move left and right as the browser window's width changes. That's easy too. Flex has another way to position components called constraints and it's probably the method you'll use the most. Constraints let certain container components (those that allow absolute positioning) shift their content according to the parameters you specify. Using constraints is a lot better than using x, y, width, and height, because it positions the components dynamically and lets them adapt to the size of the browser window. Let's start with the DataGrid. So far the DataGrid is at the edge of the browser space. Using constraints can give it some margins to make it look nicer: <mx:DataGrid left="10" top="50" right="10" bottom="10" /> The constraints in this MXML tag are named left, top, right, and bottom. Each constraint gives the position, in pixels, from that edge of the container component. The tag above says DataGrid should always be 10 pixels from the left edge, 50 pixels from the top edge, 10 pixels from the right edge, and 10 pixels from the bottom edge. Centering components uses another constraint: <mx:Text text="My Title Goes Here" horizontalCenter="0" /> The horizontalCenter constraint is the number of pixels that the center of the component should be from the center of the container. That is, when set to 0, the center of the Text component is exactly at the center of the application. If you set horizontalCenter="-20" then the center of the Text component would be shifted left of center by 20 pixels. A positive number shifts the center of the component to the right. There's a corresponding verticalCenter constraint too. Note: You don't have to use all the constraints with every control. Use only the ones that make sense. For example, to make a component hug the right edge, use right="0" but don't use left. If you do use both left and right the component will be stretched across the application.
Flex Builder The layout tool (lower right) allows you to give exact values for size, position, and constraint. Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||