java.lang.Object | |||
↳ | android.view.View | ||
↳ | android.view.ViewGroup | ||
↳ | android.support.v7.widget.GridLayout |
A layout that places its children in a rectangular grid.
The grid is composed of a set of infinitely thin lines that separate the
viewing area into cells. Throughout the API, grid lines are referenced
by grid indices. A grid with N
columns
has N + 1
grid indices that run from 0
through N
inclusive. Regardless of how GridLayout is
configured, grid index 0
is fixed to the leading edge of the
container and grid index N
is fixed to its trailing edge
(after padding is taken into account).
rowSpec
and
columnSpec
layout parameters.
Each spec defines the set of rows or columns that are to be
occupied; and how children should be aligned within the resulting group of cells.
Although cells do not normally overlap in a GridLayout, GridLayout does
not prevent children being defined to occupy the same cell or group of cells.
In this case however, there is no guarantee that children will not themselves
overlap after the layout operation completes.
orientation
,
rowCount
and
columnCount
properties.
Space
view or by setting the
leftMargin
,
topMargin
,
rightMargin
and
bottomMargin
layout parameters. When the
useDefaultMargins
property is set, default margins around children are automatically
allocated based on the prevailing UI style guide for the platform.
Each of the margins so defined may be independently overridden by an assignment
to the appropriate layout parameter.
Default values will generally produce a reasonable spacing between components
but values may change between different releases of the platform.
A child's ability to stretch is inferred from the alignment properties of
its row and column groups (which are typically set by setting the
gravity
property of the child's layout parameters).
If alignment was defined along a given axis then the component
is taken as flexible in that direction. If no alignment was set,
the component is instead assumed to be inflexible.
Multiple components in the same row or column group are considered to act in parallel. Such a group is flexible only if all of the components within it are flexible. Row and column groups that sit either side of a common boundary are instead considered to act in series. The composite group made of these two elements is flexible if one of its elements is flexible.
To make a column stretch, make sure all of the components inside it define a gravity. To prevent a column from stretching, ensure that one of the components in the column does not define a gravity.
When the principle of flexibility does not provide complete disambiguation, GridLayout's algorithms favour rows and columns that are closer to its right and bottom edges.
GONE
, as having zero width and height. This is subtly different from
the policy of ignoring views that are marked as GONE outright. If, for example, a gone-marked
view was alone in a column, that column would itself collapse to zero width if and only if
no gravity was defined on the view. If gravity was defined, then the gone-marked
view has no effect on the layout and the container should be laid out as if the view
had never been added to it.
These statements apply equally to rows as well as columns, and to groups of rows or columns.
weight
. In general, it is not therefore possible
to configure a GridLayout to distribute excess space between multiple components.
Some common use-cases may nevertheless be accommodated as follows.
To place equal amounts of space around a component in a cell group;
use CENTER
alignment (or gravity
).
For complete control over excess space distribution in a row or column;
use a LinearLayout
subview to hold the components in the associated cell group.
When using either of these techniques, bear in mind that cell groups may be defined to overlap.
See GridLayout.LayoutParams
for a full description of the
layout parameters used by GridLayout.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
GridLayout.Alignment | Alignments specify where a view should be placed within a cell group and what size it should be. | ||||||||||
GridLayout.LayoutParams | Layout information associated with each of the children of a GridLayout. | ||||||||||
GridLayout.Spec | A Spec defines the horizontal or vertical characteristics of a group of cells. |
XML Attributes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
android:alignmentMode | setAlignmentMode(int) | When set to alignMargins, causes alignment to take place between the outer boundary of a view, as defined by its margins. | |||||||||
android:columnCount | setColumnCount(int) | The maxmimum number of columns to create when automatically positioning children. | |||||||||
android:columnOrderPreserved | setColumnOrderPreserved(boolean) | When set to true, forces column boundaries to appear in the same order as column indices. | |||||||||
android:orientation | setOrientation(int) | The orientation property is not used during layout. | |||||||||
android:rowCount | setRowCount(int) | The maxmimum number of rows to create when automatically positioning children. | |||||||||
android:rowOrderPreserved | setRowOrderPreserved(boolean) | When set to true, forces row boundaries to appear in the same order as row indices. | |||||||||
android:useDefaultMargins | setUseDefaultMargins(boolean) | When set to true, tells GridLayout to use default margins when none are specified in a view's layout parameters. |
[Expand]
Inherited XML Attributes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.view.ViewGroup
| |||||||||||
From class
android.view.View
|
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | ALIGN_BOUNDS | This constant is an alignmentMode . |
|||||||||
int | ALIGN_MARGINS | This constant is an alignmentMode . |
|||||||||
int | HORIZONTAL | The horizontal orientation. | |||||||||
int | UNDEFINED | The constant used to indicate that a value is undefined. | |||||||||
int | VERTICAL | The vertical orientation. |
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.view.ViewGroup
| |||||||||||
From class
android.view.View
|
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
BASELINE | Indicates that a view should be aligned with the baselines of the other views in its cell group. | ||||||||||
BOTTOM | Indicates that a view should be aligned with the bottom edges of the other views in its cell group. | ||||||||||
CENTER | Indicates that a view should be centered with the other views in its cell group. | ||||||||||
END | Indicates that a view should be aligned with the end edges of the other views in its cell group. | ||||||||||
FILL | Indicates that a view should expanded to fit the boundaries of its cell group. | ||||||||||
LEFT | Indicates that a view should be aligned with the left edges of the other views in its cell group. | ||||||||||
RIGHT | Indicates that a view should be aligned with the right edges of the other views in its cell group. | ||||||||||
START | Indicates that a view should be aligned with the start edges of the other views in its cell group. | ||||||||||
TOP | Indicates that a view should be aligned with the top edges of the other views in its cell group. |
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.view.View
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns a new set of layout parameters based on the supplied attributes set.
| |||||||||||
Returns the alignment mode.
| |||||||||||
Returns the current number of columns.
| |||||||||||
Returns the current orientation.
| |||||||||||
Return the printer that will log diagnostics from this layout.
| |||||||||||
Returns the current number of rows.
| |||||||||||
Returns whether or not this GridLayout will allocate default margins when no
corresponding layout parameters are defined.
| |||||||||||
Returns whether or not column boundaries are ordered by their grid indices.
| |||||||||||
Returns whether or not row boundaries are ordered by their grid indices.
| |||||||||||
Call this when something has changed which has invalidated the
layout of this view.
| |||||||||||
Sets the alignment mode to be used for all of the alignments between the
children of this container.
| |||||||||||
ColumnCount is used only to generate default column/column indices when
they are not specified by a component's layout parameters.
| |||||||||||
When this property is
true , GridLayout is forced to place the column boundaries
so that their associated grid indices are in ascending order in the view. | |||||||||||
GridLayout uses the orientation property for two purposes:
| |||||||||||
Set the printer that will log diagnostics from this layout.
| |||||||||||
RowCount is used only to generate default row/column indices when
they are not specified by a component's layout parameters.
| |||||||||||
When this property is
true , GridLayout is forced to place the row boundaries
so that their associated grid indices are in ascending order in the view. | |||||||||||
When
true , GridLayout allocates default margins around children
based on the child's visual characteristics. | |||||||||||
Return a Spec,
spec , where:
To leave the start index undefined, use the value | |||||||||||
Return a Spec,
spec , where:
To leave the start index undefined, use the value | |||||||||||
Return a Spec,
spec , where:
To leave the start index undefined, use the value | |||||||||||
Return a Spec,
spec , where:
To leave the start index undefined, use the value |
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns a set of default layout parameters.
| |||||||||||
Returns a safe set of layout parameters based on the supplied layout params.
| |||||||||||
Called from layout when this view should
assign a size and position to each of its children.
| |||||||||||
Measure the view and its content to determine the measured width and the measured height. |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.view.ViewGroup
| |||||||||||
From class
android.view.View
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
android.graphics.drawable.Drawable.Callback
| |||||||||||
From interface
android.view.KeyEvent.Callback
| |||||||||||
From interface
android.view.ViewManager
| |||||||||||
From interface
android.view.ViewParent
| |||||||||||
From interface
android.view.accessibility.AccessibilityEventSource
|
When set to alignMargins, causes alignment to take place between the outer
boundary of a view, as defined by its margins. When set to alignBounds,
causes alignment to take place between the edges of the view.
The default is alignMargins.
See setAlignmentMode(int)
.
Must be one of the following constant values.
Constant | Value | Description |
---|---|---|
alignBounds | 0 | Align the bounds of the children.
See ALIGN_BOUNDS . |
alignMargins | 1 | Align the margins of the children.
See ALIGN_MARGINS . |
This corresponds to the global attribute
resource symbol alignmentMode
.
The maxmimum number of columns to create when automatically positioning children.
Must be an integer value, such as "100
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol columnCount
.
When set to true, forces column boundaries to appear in the same order
as column indices.
The default is true.
See setColumnOrderPreserved(boolean)
.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol columnOrderPreserved
.
The orientation property is not used during layout. It is only used to allocate row and column parameters when they are not specified by its children's layout paramters. GridLayout works like LinearLayout in this case; putting all the components either in a single row or in a single column - depending on the value of this flag. In the horizontal case, a columnCount property may be additionally supplied to force new rows to be created when a row is full. The rowCount attribute may be used similarly in the vertical case. The default is horizontal.
Must be one of the following constant values.
Constant | Value | Description |
---|---|---|
horizontal | 0 | Defines an horizontal widget. |
vertical | 1 | Defines a vertical widget. |
This corresponds to the global attribute
resource symbol orientation
.
The maxmimum number of rows to create when automatically positioning children.
Must be an integer value, such as "100
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol rowCount
.
When set to true, forces row boundaries to appear in the same order
as row indices.
The default is true.
See setRowOrderPreserved(boolean)
.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol rowOrderPreserved
.
When set to true, tells GridLayout to use default margins when none are specified
in a view's layout parameters.
The default value is false.
See setUseDefaultMargins(boolean)
.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol useDefaultMargins
.
This constant is an alignmentMode
.
When the alignmentMode
is set to ALIGN_BOUNDS
, alignment
is made between the edges of each component's raw
view boundary: i.e. the area delimited by the component's:
top
,
left
,
bottom
and
right
properties.
For example, when GridLayout
is in ALIGN_BOUNDS
mode,
children that belong to a row group that uses TOP
alignment will
all return the same value when their getTop()
method is called.
This constant is an alignmentMode
.
When the alignmentMode
is set to ALIGN_MARGINS
,
the bounds of each view are extended outwards, according
to their margins, before the edges of the resulting rectangle are aligned.
For example, when GridLayout
is in ALIGN_MARGINS
mode,
the quantity top - layoutParams.topMargin
is the same for all children that
belong to a row group that uses TOP
alignment.
The horizontal orientation.
The constant used to indicate that a value is undefined.
Fields can use this value to indicate that their values
have not yet been set. Similarly, methods can return this value
to indicate that there is no suitable value that the implementation
can return.
The value used for the constant (currently MIN_VALUE
) is
intended to avoid confusion between valid values whose sign may not be known.
The vertical orientation.
Indicates that a view should be aligned with the baselines
of the other views in its cell group.
This constant may only be used as an alignment in rowSpecs
.
Indicates that a view should be aligned with the bottom edges of the other views in its cell group.
Indicates that a view should be centered with the other views in its cell group.
This constant may be used in both rowSpecs
and columnSpecs
.
Indicates that a view should be aligned with the end edges of the other views in its cell group.
Indicates that a view should expanded to fit the boundaries of its cell group.
This constant may be used in both rowSpecs
and
columnSpecs
.
Indicates that a view should be aligned with the left edges of the other views in its cell group.
Indicates that a view should be aligned with the right edges of the other views in its cell group.
Indicates that a view should be aligned with the start edges of the other views in its cell group.
Indicates that a view should be aligned with the top edges of the other views in its cell group.
Returns a new set of layout parameters based on the supplied attributes set.
attrs | the attributes to build the layout parameters from |
---|
ViewGroup.LayoutParams
or one
of its descendants
Returns the alignment mode.
ALIGN_BOUNDS
or ALIGN_MARGINS
Returns the current number of columns. This is either the last value that was set
with setColumnCount(int)
or, if no such value was set, the maximum
value of each the upper bounds defined in columnSpec
.
Returns the current orientation.
HORIZONTAL
or VERTICAL
Return the printer that will log diagnostics from this layout.
Returns the current number of rows. This is either the last value that was set
with setRowCount(int)
or, if no such value was set, the maximum
value of each the upper bounds defined in rowSpec
.
Returns whether or not this GridLayout will allocate default margins when no corresponding layout parameters are defined.
true
if default margins should be allocatedReturns whether or not column boundaries are ordered by their grid indices.
true
if column boundaries must appear in the order of their indices,
false
otherwiseReturns whether or not row boundaries are ordered by their grid indices.
true
if row boundaries must appear in the order of their indices,
false
otherwiseCall this when something has changed which has invalidated the
layout of this view. This will schedule a layout pass of the view
tree. This should not be called while the view hierarchy is currently in a layout
pass (isInLayout()
. If layout is happening, the request may be honored at the
end of the current layout pass (and then layout will run again) or after the current
frame is drawn and the next layout occurs.
Subclasses which override this method should call the superclass method to handle possible request-during-layout errors correctly.
Sets the alignment mode to be used for all of the alignments between the children of this container.
The default value of this property is ALIGN_MARGINS
.
alignmentMode | either ALIGN_BOUNDS or ALIGN_MARGINS |
---|
ColumnCount is used only to generate default column/column indices when they are not specified by a component's layout parameters.
columnCount | the number of columns. |
---|
When this property is true
, GridLayout is forced to place the column boundaries
so that their associated grid indices are in ascending order in the view.
When this property is false
GridLayout is at liberty to place the horizontal column
boundaries in whatever order best fits the given constraints.
The default value of this property is true
.
columnOrderPreserved | use true to force GridLayout to respect the order
of column boundaries. |
---|
GridLayout uses the orientation property for two purposes:
HORIZONTAL
the horizontal axis is laid out first.
If your layout contains a TextView
(or derivative:
Button
, EditText
, CheckBox
, etc.) which is
in multi-line mode (the default) it is normally best to leave GridLayout's
orientation as HORIZONTAL
- because TextView
is capable of
deriving its height for a given width, but not the other way around.
Other than the effects above, orientation does not affect the actual layout operation of
GridLayout, so it's fine to leave GridLayout in HORIZONTAL
mode even if
the height of the intended layout greatly exceeds its width.
The default value of this property is HORIZONTAL
.
orientation | either HORIZONTAL or VERTICAL |
---|
Set the printer that will log diagnostics from this layout.
The default value is created by LogPrinter
.
printer | the printer associated with this layout |
---|
RowCount is used only to generate default row/column indices when they are not specified by a component's layout parameters.
rowCount | the number of rows |
---|
When this property is true
, GridLayout is forced to place the row boundaries
so that their associated grid indices are in ascending order in the view.
When this property is false
GridLayout is at liberty to place the vertical row
boundaries in whatever order best fits the given constraints.
The default value of this property is true
.
rowOrderPreserved | true to force GridLayout to respect the order
of row boundaries |
---|
When true
, GridLayout allocates default margins around children
based on the child's visual characteristics. Each of the
margins so defined may be independently overridden by an assignment
to the appropriate layout parameter.
When false
, the default value of all margins is zero.
When setting to true
, consider setting the value of the
alignmentMode
property to ALIGN_BOUNDS
.
The default value of this property is false
.
useDefaultMargins | use true to make GridLayout allocate default margins |
---|
Return a Spec, spec
, where:
spec.span = [start, start + 1]
spec.alignment = alignment
To leave the start index undefined, use the value UNDEFINED
.
start | the start index |
---|---|
alignment | the alignment |
Return a Spec, spec
, where:
spec.span = [start, start + size]
To leave the start index undefined, use the value UNDEFINED
.
start | the start |
---|---|
size | the size |
Return a Spec, spec
, where:
spec.span = [start, start + size]
spec.alignment = alignment
To leave the start index undefined, use the value UNDEFINED
.
start | the start |
---|---|
size | the size |
alignment | the alignment |
Return a Spec, spec
, where:
spec.span = [start, start + 1]
To leave the start index undefined, use the value UNDEFINED
.
start | the start index |
---|
Returns a set of default layout parameters. These parameters are requested
when the View passed to addView(View)
has no layout parameters
already set. If null is returned, an exception is thrown from addView.
Returns a safe set of layout parameters based on the supplied layout params.
When a ViewGroup is passed a View whose layout params do not pass the test of
checkLayoutParams(android.view.ViewGroup.LayoutParams)
, this method
is invoked. This method should return a new set of layout params suitable for
this ViewGroup, possibly by copying the appropriate attributes from the
specified set of layout params.
p | The layout parameters to convert into a suitable set of layout parameters for this ViewGroup. |
---|
ViewGroup.LayoutParams
or one
of its descendants
Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.
changed | This is a new size or position for this view |
---|---|
left | Left position, relative to parent |
top | Top position, relative to parent |
right | Right position, relative to parent |
bottom | Bottom position, relative to parent |
Measure the view and its content to determine the measured width and the
measured height. This method is invoked by measure(int, int)
and
should be overriden by subclasses to provide accurate and efficient
measurement of their contents.
CONTRACT: When overriding this method, you
must call setMeasuredDimension(int, int)
to store the
measured width and height of this view. Failure to do so will trigger an
IllegalStateException
, thrown by
measure(int, int)
. Calling the superclass'
onMeasure(int, int)
is a valid use.
The base class implementation of measure defaults to the background size,
unless a larger size is allowed by the MeasureSpec. Subclasses should
override onMeasure(int, int)
to provide better measurements of
their content.
If this method is overridden, it is the subclass's responsibility to make
sure the measured height and width are at least the view's minimum height
and width (getSuggestedMinimumHeight()
and
getSuggestedMinimumWidth()
).
widthSpec | horizontal space requirements as imposed by the parent.
The requirements are encoded with
View.MeasureSpec . |
---|---|
heightSpec | vertical space requirements as imposed by the parent.
The requirements are encoded with
View.MeasureSpec . |