In .NET MAUI (Multi-platform App UI), a layout is a container that defines how visual elements, such as buttons, labels, images, and other controls, are arranged within a view. Layouts control the position, size, and alignment of child elements in your application's user interface, allowing you to build complex UIs effectively.
Layouts are at the core of UI development because they allow developers to efficiently organize content in different ways, depending on the screen size, orientation, and the design requirements of the application. .NET MAUI provides several layout options, each with unique features and purposes.
There are several kinds of layouts available in .NET MAUI, each serving different use cases. Let's break them down:
StackLayout is a simple layout that arranges its child elements in a single line, either vertically or horizontally.
- Vertical StackLayout: Aligns elements from top to bottom.
- Horizontal StackLayout: Aligns elements from left to right.
Properties:
Orientation: Specifies the direction of stacking (Vertical or Horizontal).Spacing: Defines the space between each child element.
Example:
<StackLayout Orientation="Vertical" Spacing="10">
<Label Text="First Item" />
<Label Text="Second Item" />
<Button Text="Click Me!" />
</StackLayout>When to use: StackLayout is best used for simple and linear arrangements of elements, such as forms or simple lists, where order and sequence are important.
The Grid layout allows arranging elements in rows and columns, providing a table-like structure that is more flexible than StackLayout.
Properties:
RowDefinitionsandColumnDefinitions: Define the rows and columns in the Grid.Grid.RowandGrid.Column: Assign elements to specific rows and columns.
Example:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Label Text="First Item" Grid.Row="0" Grid.Column="0" />
<Label Text="Second Item" Grid.Row="0" Grid.Column="1" />
<Button Text="Click Me!" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" />
</Grid>When to use: The Grid layout is best used when you need precise control over the position of elements, especially for building complex layouts that require rows and columns, like tables or dashboard UIs.
AbsoluteLayout positions child elements using explicit coordinates, giving the developer full control over each element's position and size.
Properties:
AbsoluteLayout.LayoutBounds: Defines the bounds of an element using a rectangle structure (X, Y, Width, Height).AbsoluteLayout.LayoutFlags: Specifies how bounds are interpreted (e.g.,All,None).
Example:
<AbsoluteLayout>
<Label Text="Absolute Item" AbsoluteLayout.LayoutBounds="0.5, 0.5, 100, 50" AbsoluteLayout.LayoutFlags="PositionProportional" />
</AbsoluteLayout>When to use: AbsoluteLayout is suitable for highly custom layouts where elements need to be placed exactly in specific positions, often used in games or when the UI must look identical across different screen sizes.
FlexLayout is similar to CSS Flexbox and allows elements to wrap or grow as needed.
Properties:
Direction: Specifies the direction of the layout flow (Row, Column, etc.).Wrap: Allows elements to wrap to the next line when there's not enough space.AlignItemsandJustifyContent: Provide advanced alignment options.
Example:
<FlexLayout Direction="Row" Wrap="Wrap" JustifyContent="Center" AlignItems="Center">
<Label Text="Item 1" />
<Label Text="Item 2" />
<Button Text="Click Me!" />
</FlexLayout>When to use: FlexLayout is useful when you need a responsive layout that adapts to different screen sizes and orientations, especially for more dynamic UIs that must respond to content changes.
RelativeLayout allows the positioning of elements relative to other elements or the container itself.
Properties:
Constraint: Defines the position and size relative to other elements or the parent.
Example:
<RelativeLayout>
<Label x:Name="label1" Text="Item 1" />
<Label Text="Item 2"
RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToView, ElementName=label1, Property=Width, Factor=1}" />
</RelativeLayout>When to use: RelativeLayout is effective when you need elements to be positioned dynamically, depending on other elements' sizes or positions, for example, when building a layout that needs to adapt closely to different screen sizes or when relative positioning matters.
Choosing the right layout depends on the complexity of your UI, the desired responsiveness, and the amount of control needed over the positioning of child elements:
- Use StackLayout for simple, linear layouts.
- Use Grid when you need more structured control over rows and columns.
- Use AbsoluteLayout for precise control over element placement.
- Use FlexLayout for responsive, adaptable layouts.
- Use RelativeLayout for positioning elements dynamically, based on relationships to other elements.
Consider building a login page with a logo, input fields, and a button. You could use StackLayout for a straightforward vertical arrangement:
<StackLayout Padding="20" Spacing="15">
<Image Source="logo.png" HeightRequest="100" />
<Entry Placeholder="Username" />
<Entry Placeholder="Password" IsPassword="True" />
<Button Text="Login" />
</StackLayout>For a more complex form with additional fields and proper alignment, Grid could be used to align labels and fields neatly.
- .NET MAUI Documentation - Layouts
- Microsoft Learn - StackLayout in .NET MAUI
- Microsoft Learn - Grid Layout in .NET MAUI
- David Ortinau's Blog - Layouts in .NET MAUI
In .NET MAUI (Multi-platform App UI), StackLayout is a layout container that arranges child elements in a single line, either vertically or horizontally. It is one of the most commonly used layout types, allowing developers to align multiple controls either in a column (vertically) or in a row (horizontally). StackLayout is easy to use and serves well in scenarios where a linear arrangement is required.
The orientation of the StackLayout (whether horizontal or vertical) plays a critical role in determining how the child elements are arranged. By default, StackLayout uses vertical orientation.
-
Orientation: This property determines whether the children are stacked horizontally or vertically. The default value is
Vertical, but it can be set toHorizontalif needed. This property is crucial for deciding the direction in which child elements will be arranged.Orientation="Vertical" // or Horizontal
- Vertical Orientation: Child elements are arranged from top to bottom, one below the other.
- Horizontal Orientation: Child elements are arranged from left to right, side by side.
-
Spacing: The Spacing property defines the amount of space between each child element inside the StackLayout. By default, the spacing is set to 6 units. Increasing or decreasing the spacing affects how much distance there is between consecutive child elements.
Spacing="10"
For example, setting
Spacing="0"will remove all space between child elements, while a larger value likeSpacing="20"will provide more separation between them. This property helps to create a more organized look by controlling the inter-element spacing. -
Padding: The Padding property specifies the amount of space between the edges of the StackLayout and its child elements. Padding allows for additional space around the content, effectively controlling how close the elements are to the boundaries of the StackLayout.
Padding="20"
Padding can also be specified for each side individually using a comma-separated format (e.g.,
Padding="10, 20, 30, 40"), which represents padding for left, top, right, and bottom, respectively. -
HorizontalOptions and VerticalOptions: These properties define how the StackLayout and its child elements should be positioned and sized within their parent container. They use the LayoutOptions enumeration to control alignment and expansion behavior.
HorizontalOptions="Center" VerticalOptions="FillAndExpand"
The commonly used values for these options are:
- Start: Aligns the element to the start (left or top).
- Center: Aligns the element to the center.
- End: Aligns the element to the end (right or bottom).
- Fill: Stretches the element to fill the available space.
- Expand: Allows the element to take up additional space if available.
Using combinations like
FillAndExpandallows the element to both stretch and occupy additional available space, which can be very useful for flexible UI designs. -
IsVisible: The IsVisible property controls whether the StackLayout and its child elements are visible or hidden. If set to
false, the StackLayout and all its children will be hidden from the UI but still occupy space in the layout.IsVisible="True" // or False
-
BackgroundColor: The BackgroundColor property is used to set the background color of the StackLayout, allowing developers to visually separate different parts of the UI.
BackgroundColor="LightGray"
Let's look at an example of a simple StackLayout used in a .xaml page of a .NET MAUI application:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiAppDemo.MainPage">
<StackLayout Orientation="Vertical" Spacing="15" Padding="20">
<Label Text="Welcome to MAUI!"
FontSize="Large"
HorizontalOptions="Center" />
<Entry Placeholder="Enter your name" />
<Button Text="Click Me" />
</StackLayout>
</ContentPage>In this example:
- Orientation is set to
Vertical, meaning the child elements are arranged from top to bottom. - The Spacing between the elements is set to 15 units.
- Padding is used to create some space inside the boundaries of the StackLayout.
There is a Label, an Entry, and a Button, each stacked in the order they appear within the StackLayout.
You should use StackLayout when:
- You need a simple linear layout that arranges elements either vertically or horizontally.
- You want an easy way to manage a column or row of UI elements.
- Your layout needs are not complex, and you do not need features like relative positioning or nested hierarchies that would require more advanced layouts.
However, it's important to note that StackLayout can incur performance issues if you have a large number of child elements because all child elements are stacked linearly and can lead to excessive memory usage. In such cases, using Grid or CollectionView might be a better choice.
- Orientation: Defines the stacking direction.
- Spacing: Sets the space between child elements.
- Padding: Adds space inside the layout container's boundaries.
- HorizontalOptions / VerticalOptions: Manages alignment and positioning within the parent container.
