Xamarin.Forms Guides | Samples

Xamarin.Forms.StackLayout Class

A Layout`1 that positions child elements in a single line which can be oriented vertically or horizontally.

See Also: StackLayout

Syntax

public class StackLayout : Layout<View>

Remarks

Because StackLayout layouts override the bounds on their child elements, application developers should not set bounds on them.

The following example code, adapted from the FormsGallery example shows how to create a new StackLayout with children that explore many of the layout behaviors of StackLayout:

C# Example

StackLayout stackLayout = new StackLayout
{
    Spacing = 0,
    VerticalOptions = LayoutOptions.FillAndExpand,
    Children = 
    {
        new Label
        {
            Text = "StackLayout",
            HorizontalOptions = LayoutOptions.Start
        },
        new Label
        {
            Text = "stacks its children",
            HorizontalOptions = LayoutOptions.Center
        },
        new Label
        {
            Text = "vertically",
            HorizontalOptions = LayoutOptions.End
        },
        new Label
        {
            Text = "by default,",
            HorizontalOptions = LayoutOptions.Center
        },
        new Label
        {
            Text = "but horizontal placement",
            HorizontalOptions = LayoutOptions.Start
        },
        new Label
        {
            Text = "can be controlled with",
            HorizontalOptions = LayoutOptions.Center
        },
        new Label
        {
            Text = "the HorizontalOptions property.",
            HorizontalOptions = LayoutOptions.End
        },
        new Label
        {
            Text = "An Expand option allows one or more children " +
                   "to occupy the an area within the remaining " +
                   "space of the StackLayout after it's been sized " +
                   "to the height of its parent.",
            VerticalOptions = LayoutOptions.CenterAndExpand,
            HorizontalOptions = LayoutOptions.End
        },
        new StackLayout
        {
            Spacing = 0,
            Orientation = StackOrientation.Horizontal,
            Children = 
            {
                new Label
                {
                    Text = "Stacking",
                },
                new Label
                {
                    Text = "can also be",
                    HorizontalOptions = LayoutOptions.CenterAndExpand
                },
                new Label
                {
                    Text = "horizontal.",
                },
            }
        }
    }
};

XAML for Xamarin.Forms supports the following properties for the StackLayout class:

PropertyValue
Orientation

Horizontal or Vertical. The default is Vertical.

Spacing

An integer or decimal.

Requirements

Namespace: Xamarin.Forms
Assembly: Xamarin.Forms.Core (in Xamarin.Forms.Core.dll)
Assembly Versions: 1.0.0.0, 1.1.0.0, 1.2.0.0, 1.3.0.0, 1.3.3.0, 1.4.0.0, 1.5.0.0, 2.0.0.0

The members of Xamarin.Forms.StackLayout are listed below.

See Also: Inherited members from Xamarin.Forms.Layout<Xamarin.Forms.View>

Public Constructors

Initializes a new instance of the StackLayout class.

Public Fields

static readonly
OrientationPropertyBindableProperty. Identifies the Orientation bindable property.
static readonly
SpacingPropertyBindableProperty. Identifies the Spacing bindable property.

Public Properties

OrientationStackOrientation. Gets or sets the value which indicates the direction which child elements are positioned.
SpacingDouble. Gets or sets a value which indicates the amount of space between each child element.

Protected Methods

override
InvalidateMeasure()
Invalidates the layout.
override
LayoutChildren(Double, Double, Double, Double)
Positions and sizes the children of a StackLayout.
override
OnSizeRequest(Double, Double) : SizeRequest
This method is called during the measure pass of a layout cycle to get the desired size of the StackLayout.