A (175)

(Tuis.) #1

262 CHAPTER 8: Android UI Design: Using Advanced ViewGroup Layout Containers


It is important to note that while grid cells should not normally be defined to overlap each other in
GridLayouts, the GridLayout class will not prevent child UI widgets from being defined in such a way,
so widgets can occupy the same cell, or can span a group of cells. This allows UI designers a lot of
flexibility for using the GridLayout container to create scalable (morphing to fit different screen sizes
and shapes) grid-based UI layout designs.


Note that if you design with more than one child widget per grid cell, or with more than one grid cell
per child widget (termed: spanning), there is no guarantee that child UI widgets will not overlap after
the GridLayout UI engine evaluates your parameters in its GridLayout computation operation. For
this reason, make sure that you test your GridLayout UI designs thoroughly using several different
device emulators and device orientations.


GridLayout Location, Flexibility, and Gravity


If your child UI widget tag doesn’t include any parameters specifying the row and column index
of the grid cell which it intends to occupy, then the GridLayout class algorithm will assign its cell
location for it automatically. This is done based on the GridLayout container definition parameters
in a parent tag, along with the GridLayout orientation, rowCount, and columnCount
attributes or property (parameters) global settings.


White space between child UI elements in a GridLayout can be specified either by using an instance of
the Android SpaceView class (i.e. a SpaceView object), or by using android:layout_margin parameters.


When the parent tag (parameters in the parent tag signify that the parameter is a
global GridLayout engine setting) XML parameter android:useDefaultMargins is set to a Boolean
value of true, then default margins around child UI widgets are automatically allocated, based on the
UI style guide for the Android platform running on the device at the time.


If you wanted to “toggle” this particular GridLayout parameter dynamically, you would do this using
your Java code, by implementing the .setUseDefaultMargins() method call, passing a true or a false
Boolean data value. The UseDefaultMargins attribute’s value would be said to be set statically
(the opposite of dynamically), if it’s initial (starting) data value is specified by using the GridLayout
XML layout definition. The reason that this is considered to be “static” is that the XML UI definition
is evaluated during “inflation” before the application is put into use by the user, whereas the Java
method is used in a “dynamic” fashion in response to the user’s use of the application, and is
therefore evaluated dynamically while the application is “in play,” and not prior to its use.


It is important to note that each of the four (left, right, top, bottom) margin settings which will
be automatically defined using the android:useDefaultMargins=true specification, may each be
independently overridden via the assignment of one of these four margin parameters, allowing a
GridLayout designer fine-tuned margin control.


It is important to note that the GridLayout class distributes excess space between child UI widgets
based upon a priority principle, and not using the principle of weight, which is currently unique to
the LinearLayout container type. For this reason, a child UI widget’s ability to “stretch” is inferred
from the alignment properties for its row and column groupings.


Grid cell alignment characteristics are set by specifying the child widget’s gravity property
(parameter) for each child UI element. This is done by using the android:layout_gravity parameter. If
alignment is defined along a given X or Y grid axis, then that child UI widget would be considered to
be “flexible” regarding that direction.

Free download pdf