Constraint Based Layout
Constraint Based Layout
Constraint-Based Layout
Objectives
After completing this unit, you should be able to:
• Absolutely position controls and containers in Application, Panel and
Canvas containers
• Create constraint-based layouts using Flex Builder’s Design mode
• Create constraint-based layouts by typing MXML
• Nest constraint-based layouts
Topics
In this unit, you will learn about the following topics:
• Understanding absolute positioning
• Positioning components within a Canvas container
• Creating a constraint-based layout using Flex Builder
• Creating a constraint-based layout via MXML
• Using Enhanced Constraints
• Using constraint-based layouts within nested containers
Note: The values for the x and y coordinates can be a positive or negative
Number datatype. You can use negative values to place a child outside the
visible area of the container, and then use ActionScript to move the child to
the visible area, possibly in response to an event.
Steps
Import the Constraints Project
1. Select File > Import > Flex Project.
2. Import project from Archive file.
• Browse to: <installDirectory>/adobeTraining/f3rca/
[Link]
• Click Open
• Click Finish
3. Close all unrelated projects.
4. Open the ConstraintLayout_wt1.mxml file from the src folder at the
root of the project.
16. Run the application. You should see the seating chart with the text overlay.
17. Switch to Source mode and review the code with your instructor.
Figure 5: If a component is set to a percentage width, it will resize to ensure that the anchor rules stay true
Select an anchor
Figure 6: The easiest way to create a constraint-based layout is to set the constraint
anchors using Flex Builder’s Design mode
Note: You may find that in some instances, you get overlapping components
with constraint-based layouts. If this is not the behavior you want, add
additional anchors to the overlapping elements or set a minimum width and
height for the container.
• Use care with component width and height properties when using
constraint-based layout
■ If you do not specifically define the width and height of some
components, like the Text control, you will find that they may be
truncated
– Setting the width or height to 100% may solve some of
the issues
Steps
1. Open the ConstraintLayout_wt2.mxml file from the src folder at the
root of the project.
2. Run the application. You should see details regarding Chef Ipsum’s newly
released DVD. This item is the second item in the Cafe Store so both next
and previous button controls are displayed. Resize the browser and note
that the positions of the components on screen do not change.
Note: The pixel number that is associated with the constraint may be different
on your computer. It is dependent on the size of your Design mode display.
4. Run the application. You should see, as you resize the browser, that the
Label control always stays centered horizontally in the browser. However,
it does sometimes overlap the prev and next buttons.
6. Select the next button. Anchor the top right and set the right offset to 25
pixels. This will constrain the button to the right of its container.
7. Run the application. You should see, as you resize the browser, that the
prev and next buttons stay in the top left and right corners of the browser,
respectively. However, if the browser is resized small enough, you will still
get overlapping controls.
12. Make sure the Text control is still selected and set the top anchor.
Note: Design mode does not refresh the display to show the entire paragraph
of text.
< mx :F o rm i d =” m yF or m ” le f t= ”2 0 ” r ig ht = ”2 0” / >
< mx :P a ne l
w id th = "2 50 " h e ig ht = "2 00 "
l ay ou t =" ab s ol u te "
h or iz o nt al C en t er =" 0 "
v er ti c al Ce n te r =" 21 " >
[ .. .]
< /m x: P an el >
Steps
1. Open the ConstraintLayout_wt3.mxml file from the src folder at the
root of the project.
2. Run the application. You should see details regarding Chef Ipsum’s newly
released DVD. This application is similar to the previous walkthrough
and will have similar results.
Note: A best practice is to ensure the sum of all percent constraints is less than
or equal to 100%.
<m x: C an va s >
<m x :c on s tr a in tC o lu mn s >
<m x: C on s tr ai n tC ol u mn id =" c ol 1"
w id t h= " 21 2" / >
< mx : Co n st ra i nt Co l um n i d= " co l2 "
w id t h= " 10 0% " />
</ m x: co n st r ai nt C ol um n s>
<m x :c on s tr a in tR o ws >
< mx : Co ns t ra i nt Ro w i d =" r ow 1" he i gh t =" 80 % "/ >
< mx : Co ns t ra i nt Ro w i d =" r ow 2" he i gh t =" 20 % "/ >
</ m x: co n st r ai nt R ow s>
. ..
</ mx : Ca nv a s>
Note: You may use any combination of qualified and unqualified constraint
properties but this may cause some constraint properties to be overridden.
Steps
1. Open the ConstraintLayout_demo.mxml from the src folder at the root
of the project.
2. Examine the code with your instructor.
There are two constraint columns using percentage space allocation. There
are three constraint rows using fixed space allocation. There are three
controls that will be constrained by the columns and rows within a Canvas
container.
Steps
1. Open the ConstraintLayout_wt4.mxml file in the src folder at the root
of the project.
2. Run the application. You should see the Cafe Townsend application with
the three panels on the right. The main content area is a blank white space,
but as you increase or decrease the browser size, that area responds to the
change.
< mx :A p pl ic a ti o n
xm ln s :m x= " ht t p: // w ww .a d ob e .c om / 20 06 / mx m l"
xm ln s :c om p =" * "
la yo u t= "v e rt i ca l"
ba ck g ro un d Co l or =" # 00 00 0 0"
ba ck g ro un d Al p ha =" 0 "
th em e Co lo r =" h al oO r an ge " >
4. Locate the comment on line 42 that prompts you to add the custom
component.
5. On line 43, call the CafeStore custom component, which is the Cafe
Store product display from the previous two walkthroughs. The code
should appear as follows.
<m x: V Bo x
p ad d in g Ri gh t =" 8"
p ad d in g To p= " 8"
p ad d in g Bo tt o m= "8 "
w id t h= " 10 0% " >
< !- - A d d cu s to m c om p on en t h er e - - >
< co m p: C af eS t or e / >
</ mx : VB ox >