Android Guides | Samples

Android.Widget.GridLayout Class

A layout that places its children in a rectangular grid.

See Also: GridLayout

Syntax

[Android.Runtime.Register("android/widget/GridLayout", DoNotGenerateAcw=true)]
public class GridLayout : ViewGroup

Remarks

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).

Row and Column Specs

Children occupy one or more contiguous cells, as defined by their NoType:android/widget/GridLayout$LayoutParams;Href=../../../reference/android/widget/GridLayout.LayoutParams.html#rowSpec and NoType:android/widget/GridLayout$LayoutParams;Href=../../../reference/android/widget/GridLayout.LayoutParams.html#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.

Default Cell Assignment

If a child does not specify the row and column indices of the cell it wishes to occupy, GridLayout assigns cell locations automatically using its: GridLayout.Orientation, GridLayout.RowCount and GridLayout.ColumnCount properties.

Space

Space between children may be specified either by using instances of the dedicated Space view or by setting the NoType:android/view/ViewGroup$MarginLayoutParams;Href=../../../reference/android/view/ViewGroup.MarginLayoutParams.html#leftMargin, NoType:android/view/ViewGroup$MarginLayoutParams;Href=../../../reference/android/view/ViewGroup.MarginLayoutParams.html#topMargin, NoType:android/view/ViewGroup$MarginLayoutParams;Href=../../../reference/android/view/ViewGroup.MarginLayoutParams.html#rightMargin and NoType:android/view/ViewGroup$MarginLayoutParams;Href=../../../reference/android/view/ViewGroup.MarginLayoutParams.html#bottomMargin layout parameters. When the GridLayout.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.

Excess Space Distribution

As of API 21, GridLayout's distribution of excess space accomodates the principle of weight. In the event that no weights are specified, the previous conventions are respected and columns and rows are taken as flexible if their views specify some form of alignment within their groups.

The flexibility of a view is therefore influenced by its alignment which is, in turn, typically defined by setting the NoType:android/widget/GridLayout$LayoutParams;Href=../../../reference/android/widget/GridLayout.LayoutParams.html#setGravity(int) property of the child's layout parameters. If either a weight or alignment were defined along a given axis then the component is taken as flexible in that direction. If no weight or 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 weight or a gravity. To prevent a column from stretching, ensure that one of the components in the column does not define a weight or 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. To be more precise, GridLayout treats each of its layout parameters as a constraint in the a set of variables that define the grid-lines along a given axis. During layout, GridLayout solves the constraints so as to return the unique solution to those constraints for which all variables are less-than-or-equal-to the corresponding value in any other valid solution.

Interpretation of GONE

For layout purposes, GridLayout treats views whose visibility status is ViewStates.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. GONE views are taken to have zero weight during excess space distribution.

These statements apply equally to rows as well as columns, and to groups of rows or columns.

See NoType:android/widget/GridLayout$LayoutParams;Href=../../../reference/android/widget/GridLayout.LayoutParams.html for a full description of the layout parameters used by GridLayout.

[Android Documentation]

Requirements

Namespace: Android.Widget
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 14

The members of Android.Widget.GridLayout are listed below.

See Also: ViewGroup

Public Constructors

Protected Constructors

A constructor used when creating managed representations of JNI objects; called by the runtime.

Public Fields

const
AlignBoundsGridAlign (0). This constant is an GridLayout.AlignmentMode.
const
AlignMarginsGridAlign (1). This constant is an GridLayout.AlignmentMode.
const
HorizontalGridOrientation (0). The horizontal orientation.
const
UndefinedInt32 (-2147483648). The constant used to indicate that a value is undefined.
const
VerticalGridOrientation (1). The vertical orientation.

Public Properties

AlignmentModeGridAlign. Returns the alignment mode.
[read-only]
static
BaselineAlighmentGridLayout+Alignment. Indicates that a view should be aligned with the baselines of the other views in its cell group.
[read-only]
static
BottomAlighmentGridLayout+Alignment. Indicates that a view should be aligned with the bottom edges of the other views in its cell group.
[read-only]
static
CenterGridLayout+Alignment. Indicates that a view should be centered with the other views in its cell group.
ColumnCountInt32. Returns the current number of columns.
ColumnOrderPreservedBoolean. Returns whether or not column boundaries are ordered by their grid indices.
[read-only]
static
EndGridLayout+Alignment. Indicates that a view should be aligned with the end edges of the other views in its cell group.
[read-only]
static
FillGridLayout+Alignment. Indicates that a view should expanded to fit the boundaries of its cell group.
[read-only]
static
LeftAlighmentGridLayout+Alignment. Indicates that a view should be aligned with the left edges of the other views in its cell group.
OrientationGridOrientation. Returns the current orientation.
[read-only]
static
RightAlighmentGridLayout+Alignment. Indicates that a view should be aligned with the right edges of the other views in its cell group.
RowCountInt32. Returns the current number of rows.
RowOrderPreservedBoolean. Returns whether or not row boundaries are ordered by their grid indices.
[read-only]
static
StartGridLayout+Alignment. Indicates that a view should be aligned with the start edges of the other views in its cell group.
[read-only]
static
TopAlighmentGridLayout+Alignment. Indicates that a view should be aligned with the top edges of the other views in its cell group.
UseDefaultMarginsBoolean. Returns whether or not this GridLayout will allocate default margins when no corresponding layout parameters are defined.

Protected Properties

[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

static
InvokeSpec(Int32) : GridLayout+Spec
Return a Spec, spec, where:
  • spec.span = [start, start + 1]
static
InvokeSpec(Int32, GridLayout+Alignment) : GridLayout+Spec
Return a Spec, spec, where:
  • spec.span = [start, start + 1]
  • spec.alignment = alignment
static
InvokeSpec(Int32, Int32) : GridLayout+Spec
Return a Spec, spec, where:
  • spec.span = [start, start + size]
static
InvokeSpec(Int32, Single) : GridLayout+Spec
Equivalent to: spec(start, 1, weight).
static
InvokeSpec(Int32, GridLayout+Alignment, Single) : GridLayout+Spec
Equivalent to: spec(start, 1, alignment, weight).
static
InvokeSpec(Int32, Int32, GridLayout+Alignment) : GridLayout+Spec
Equivalent to: spec(start, size, alignment, 0f).
static
InvokeSpec(Int32, Int32, Single) : GridLayout+Spec
Equivalent to: spec(start, 1, default_alignment, weight) - where default_alignment is specified in NoType:android/widget/GridLayout$LayoutParams;Href=../../../reference/android/widget/GridLayout.LayoutParams.html.
static
InvokeSpec(Int32, Int32, GridLayout+Alignment, Single) : GridLayout+Spec
Return a Spec, spec, where:
  • spec.span = [start, start + size]
  • spec.alignment = alignment
  • spec.weight = weight

Protected Methods

override
OnLayout(Boolean, Int32, Int32, Int32, Int32)
Called from layout when this view should assign a size and position to each of its children.