Xamarin.Forms StackLayout Tutorial
Before attempting this tutorial, you should have successfully completed the:
- Build your first Xamarin.Forms app quickstart.
In this tutorial, you learn how to:
- Create a Xamarin.Forms
StackLayout
in XAML. - Specify the orientation of the
StackLayout
. - Control child view alignment and expansion within the
StackLayout
.
You will use Visual Studio 2019, or Visual Studio for Mac, to create a simple application that demonstrates how to align controls within a StackLayout
. The following screenshots show the final application:
You'll also use XAML Hot Reload for Xamarin.Forms to see UI changes without rebuilding your application.
Create a stacklayout
A StackLayout
is a layout that organizes its children in a one-dimensional stack, either horizontally or vertically. By default, a StackLayout
is oriented vertically.
To complete this tutorial you should have Visual Studio 2019 (latest release), with the Mobile development with .NET workload installed. In addition, you will require a paired Mac to build the tutorial application on iOS. For information about installing the Xamarin platform, see Installing Xamarin. For information about connecting Visual Studio 2019 to a Mac build host, see Pair to Mac for Xamarin.iOS development.
Launch Visual Studio, and create a new blank Xamarin.Forms app named StackLayoutTutorial.
Important
The C# and XAML snippets in this tutorial requires that the solution is named StackLayoutTutorial. Using a different name will result in build errors when you copy code from this tutorial into the solution.
For more information about the .NET Standard library that gets created, see Anatomy of a Xamarin.Forms application in the Xamarin.Forms Quickstart Deep Dive.
In Solution Explorer, in the StackLayoutTutorial project, double-click MainPage.xaml to open it. Then, in MainPage.xaml, remove all of the template code and replace it with the following code:
<?xml version="1.0" encoding="utf-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="StackLayoutTutorial.MainPage"> <StackLayout Margin="20,35,20,25"> <Label Text="The StackLayout has its Margin property set, to control the rendering position of the StackLayout." /> <Label Text="The Padding property can be set to specify the distance between the StackLayout and its children." /> <Label Text="The Spacing property can be set to specify the distance between views in the StackLayout." /> </StackLayout> </ContentPage>
This code declaratively defines the user interface for the page, which consists of three
Label
instances in aStackLayout
. TheStackLayout
positions its child views (theLabel
instances) in a single line, which is oriented vertically by default. In addition, theMargin
property indicates the rendering position of theStackLayout
within theContentPage
.Note
In addition to the
Margin
property,Padding
andSpacing
properties can also be set on aStackLayout
. ThePadding
property value specifies the distance between views in theStackLayout
, and theSpacing
property value specifies the amount of space between each child element in theStackLayout
. For more information, see Margin and Padding.In the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen remote iOS simulator or Android emulator:
For more information about the
StackLayout
, see Xamarin.Forms StackLayout.
Specify orientation
In MainPage.xaml, modify the
StackLayout
declaration so it aligns its children horizontally, instead of vertically:<StackLayout Margin="20,35,20,25" Orientation="Horizontal"> <Label Text="The StackLayout has its Margin property set, to control the rendering position of the StackLayout." /> <Label Text="The Padding property can be set to specify the distance between the StackLayout and its children." /> <Label Text="The Spacing property can be set to specify the distance between views in the StackLayout." /> </StackLayout>
This code sets the
Orientation
property toHorizontal
.If the application is still running, save the changes to the file and the application user interface will automatically be updated in your simulator or emulator. Otherwise, in the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen remote iOS simulator or Android emulator:
Note that the
Label
instances within theStackLayout
are now aligned horizontally, instead of vertically.
Control alignment and expansion
The size and position of child views within a StackLayout
depend upon the values of the child views' HeightRequest
and WidthRequest
properties, and the values of the HorizontalOptions
and VerticalOptions
properties.
The HorizontalOptions
and VerticalOptions
properties can be set to fields from the LayoutOptions
struct, which encapsulates two layout preferences:
- Alignment – the child view's preferred alignment, which determines its position and size within its parent layout.
- Expansion – indicates if the child view should use extra space, if it's available (used only by a
StackLayout
).
In MainPage.xaml, modify the
StackLayout
declaration to set alignment and expansion options for eachLabel
:<StackLayout Margin="20,35,20,25"> <Label Text="Start" HorizontalOptions="Start" BackgroundColor="Gray" /> <Label Text="Center" HorizontalOptions="Center" BackgroundColor="Gray" /> <Label Text="End" HorizontalOptions="End" BackgroundColor="Gray" /> <Label Text="Fill" HorizontalOptions="Fill" BackgroundColor="Gray" /> <Label Text="StartAndExpand" VerticalOptions="StartAndExpand" BackgroundColor="Gray" /> <Label Text="CenterAndExpand" VerticalOptions="CenterAndExpand" BackgroundColor="Gray" /> <Label Text="EndAndExpand" VerticalOptions="EndAndExpand" BackgroundColor="Gray" /> <Label Text="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="Gray" /> </StackLayout>
This code sets alignment preferences on the first four
Label
instances, and expansion preferences on the final fourLabel
instances. TheStart
,Center
,End
, andFill
fields are used to define the alignment of theLabel
instances within the parentStackLayout
. TheStartAndExpand
,CenterAndExpand
,EndAndExpand
, andFillAndExpand
fields are used to define the alignment preference, and whether the view will occupy more space if available within the parentStackLayout
.Note
The default value of a view's
HorizontalOptions
andVerticalOptions
properties isFill
.If the application is still running, save the changes to the file and the application user interface will automatically be updated in your simulator or emulator. Otherwise, in the Visual Studio toolbar, press the Start button (the triangular button that resembles a Play button) to launch the application inside your chosen remote iOS simulator or Android emulator:
A
StackLayout
only respects the alignment preferences on child views that are in the opposite direction to theStackLayout
orientation. Therefore, theLabel
child views within the vertically orientedStackLayout
set theirHorizontalOptions
properties to one of the alignment fields:Start
, which positions theLabel
on the left hand side of theStackLayout
.Center
, which centers theLabel
in theStackLayout
.End
, which positions theLabel
on the right hand side of theStackLayout
.Fill
, which ensures that theLabel
fills the width of theStackLayout
.
A
StackLayout
can only expand child views in the direction of its orientation. Therefore, the vertically orientedStackLayout
can expandLabel
child views that set theirVerticalOptions
properties to one of the expansion fields. This means that, for vertical alignment, eachLabel
occupies the same amount of space within theStackLayout
. However, only the finalLabel
, which sets itsVerticalOptions
property toFillAndExpand
has a different size.Important
When all the space in a
StackLayout
is used, expansion preferences have no effect.In Visual Studio, stop the application.
For more information about alignment and expansion, see Layout Options in Xamarin.Forms.
Congratulations!
Congratulations on completing this tutorial, where you learned how to:
- Create a Xamarin.Forms
StackLayout
in XAML. - Specify the orientation of the
StackLayout
. - Control child view alignment and expansion within the
StackLayout
.
Next steps
To learn more about the basics of creating mobile applications with Xamarin.Forms, continue to the Label tutorial.
Related links
Have an issue with this section? If so, please give us some feedback so we can improve this section.