CHAPTER 8: Android UI Design: Using Advanced ViewGroup Layout Containers 267
Table 8-4. GridLayout Row and Column Specification Parameter Default Data Value for Grid Cells (Child Widgets)
XML Parameter Used to Set: Object Data Field Name: Default Value:
android:layout_width PARAMETER IS LEFT UNSPECIFIED WRAP_CONTENT
android:layout_height PARAMETER IS LEFT UNSPECIFIED WRAP_CONTENT
android:layout_row rowSpec.row UNDEFINED
android:layout_column columnSpec.column UNDEFINED
android:layout_rowSpan rowSpec.rowSpan 1
android:layout_columnSpan columnSpec.columnSpan 1
android:layout_gravity rowSpec.alignment BASELINE
android:layout_gravity columnSpec.alignment START
Table 8-5. GridLayout Margin Specification Parameters Default Data Values (Based on useDefaultMargins Flag)
Margin XML Parameter: useDefaultMargins=false useDefaultMargins=true
android:layout_topMargin 0 UNDEFINED
android:layout_bottomMargin 0 UNDEFINED
android:layout_leftMargin 0 UNDEFINED
android:layout_rightMargin 0 UNDEFINED
As you can see, you do not need to specify layout_width or layout_height for child View objects in
a GridLayout. The WRAP_CONTENT and MATCH_PARENT constants are evaluated in the same way,
as they’re considered to be the same thing in a GridLayout. You can thus consider these constants
to be hard-coded in the GridLayout algorithms. Be advised that other than with GridLayout, you
must always define layout_width and layout_height, so don’t get used to this luxury unless you
intend to develop UI designs only using the GridLayout class, which would be a shame, as Android
has many more powerful layout container classes based on the ViewGroup superclass.
Row and column specifications can be left as undefined, as they will be automatically calculated by
the GridLayout algorithm, if they have not been specified. These should be specified if you know the
grid dimensions for your UI design, because defining a static (fixed) GridLayout dimension is more
memory- and processor-efficient. rowSpan and columnSpan variables default to a value of one,
which essentially equates to “do not span.” We will implement a “span” in our example, later on in
this chapter, so that you can see exactly how the GridLayout class’s span functionality works.
Default alignment for rows is BASELINE, which is quite logical if you think about it, and the default
alignment for columns is START, which is also the alignment constant that I would have guessed
would be used as the default.
The GridLayout cell margins will always default to zero if you specify an android:useDefaultMargins
="false" parameter in your parent GridLayout tag. If you specify an android:useDefaultMargins
="true" parameter in the parent GridLayout tag, the margins will remain “undefined.” What
undefined means is that your cell margins will be controlled by the GridLayout algorithm.