A (175)

(Tuis.) #1
CHAPTER 8: Android UI Design: Using Advanced ViewGroup Layout Containers 261

The GridLayout class hierarchy begins with a Java Object master class, and progresses from a View
class to a ViewGroup class to the GridLayout class, and so the GridLayout class “inherits” all of
the methods, constants, and attributes (parameters) of the View and ViewGroup classes. The class
hierarchy can be visualized as follows:


java.lang.Object



android.view.View
android.view.ViewGroup
android.widget.GridLayout



The Android GridLayout class is a public class, extending the ViewGroup superclass. It is referenced
by using the import android.widget.GridLayout statement at the top of an Android Activity
subclass, as you will soon see.


Within the GridLayout class, your grid lines will be referenced using grid indices. A grid with a
Y number of columns will have Y+1 grid indices, which will be numbered starting at 0 and run
through Y as the last column index. Regardless of how a GridLayout is constructed, grid index 0 will
be fixed to the leading edge of the layout container, and grid index Y will be fixed to the trailing
edge, after any padding values are applied.


Using the rowSpec and columnSpec Parameters


A GridLayout’s child UI widgets usually span one cell, but can also span more than one cell. This
is defined by the rowSpec and columnSpec GridLayout class parameters. These parameters are
specified inside of the child UI widget tags inside of the parent GridLayout tag. We will learn about all
of the grid layout parameters next, when we cover the Java nested class which contains these. This
class is called GridLayout.LayoutParams.


Note Nested Classes in Java are classes which are defined inside of another class. If the class is defined
using the static keyword, it is called a “nested class,” and if not, it is called an “inner class.” We will be
creating an inner class later on in this book. Many of the View and ViewGroup classes use nested classes,
which are declared as public static abstract classes, because they provide these parameters which are
ultimately used in your XML definitions. The primary reason for a nested class is for organization, as nested
classes in Android will often be viewed as “helper” classes, and need to be logically grouped together. The
reason the class names are separated using a period character is because that is how the path is defined in
Java, and so a class inside of another class, such as the LayoutParams class which is inside of the GridLayout
class, would thus be referenced as GridLayout.LayoutParams.

You can use each of the GridLayout’s “Spec” parameters to define a set of rows (android:rowSpec),
or columns (android:columnSpec), which you define as being occupied by your child widget (View
objects) tags. You can also specify how these child UI widgets are going to be aligned within the
resulting set or “group” of grid cells.

Free download pdf