Demonstrates how to use ExplicitLayout to:
- Load layout constraints from a properties file
- Define several variant layouts and switch between them at runtime
- Edit constraints without recompiling
Note: If you want to define layout variants without using a properties file, see Ratio Trigger example.
Possible uses of these features include:
- Creating variant layouts for different locales
- Creating variant layouts for different user preferences or abilities
- Creating variant layouts depending on component visibility
- Creating variant layouts based on container width / height ratio
- Dynamically generating layouts and storing them in a human readable format
- Extending API to define your layout using XML or a database
- Using the same layout definition for different containers
(e.g. container's with same or similar layouts, but unrelated in
the class hierarchy and possibly contain different components)
Instructions
The "layout.properties" file used in this example defines three layout variants:
default (null), "middle" and "right". To switch between variants,
type the variant name (or blank for default) in the text field provided
in the Constraints Editor and hit [enter] or click Layout
You can also edit the constraints in the Constraints Editor.
A constraints attribute key has the format
component_name.attribute for the default variant or
component_name.variant.attribute for the specified variant.
The constraints attribute values are specified using the
ExplicitLayout API and a syntax similar to Java language.
The main differences include:
- Components are identified using the name specified in the
corresponding ExplicitConstraints constructor
- Arrays of components or expressions are specified using the syntax
{a, b, c} rather than new Component[]{a,b,c} or new Expression[]{a,b,c}
- Some of the properties may define values for use in other properties.
To include the value of one property in the value of another, enclose the key
in angle brackets.
For further details, see API documentaion.
Keys are sorted alphabetically. To insert a new key, use the empty row at the
bottom of the table. To delete a key, delete the text in the corresponding cell and
hit [Enter]. To update the layout using your modified properties, enter the variant in
the text field provided and click Layout in Constraints Editor
There are two common types of error you may get when editing constraints:
- Invalid Constraints Attribute - Usually an syntax error in the attribute definition.
- Illegal State - Usually the result of an attribute being defined in terms of itself.
To disgard changes and reload the layout.properties file, click Reload in
Constraints Editor