MAUI Layout

Layout in .NET MAUI (Multi-platform App UI) is fundamental to arranging UI elements on the screen. MAUI provides a rich set of layout containers that enable you to create flexible and responsive user interfaces that adapt to different screen sizes and orientations across iOS, Android, macOS, and Windows.

Core Layout Concepts

MAUI layouts are based on a hierarchical structure where parent layout containers arrange their child elements. You can nest layouts within each other to build complex UIs.

Understanding Measurement and Arrangement

MAUI's layout system works in two passes:

  1. Measure: Each element is measured to determine its desired size.
  2. Arrange: Each element is positioned on the screen based on its measured size and the layout container's rules.

Common Layout Containers

Here are some of the most frequently used layout containers in .NET MAUI:

StackLayout

StackLayout arranges its children in a single horizontal or vertical line. It's ideal for simple lists of items.

Example Usage


<StackLayout Orientation="Vertical" Spacing="10" Padding="10">
    <Label Text="First Item" />
    <Button Text="Click Me" />
    <Image Source="dotnet_bot.png" />
</StackLayout>
                

This will arrange the Label, Button, and Image vertically, with 10 device-independent units of space between them.

Grid

Grid arranges its children into rows and columns, similar to an HTML table. It's powerful for creating structured layouts.

Example Usage


<Grid RowDefinitions="Auto,*,Auto" ColumnDefinitions="Auto,*" RowSpacing="5" ColumnSpacing="5" Padding="10">
    <Label Grid.Row="0" Grid.Column="0" Text="Header:" />
    <Label Grid.Row="0" Grid.Column="1" Text="Page Title" FontAttributes="Bold" />
    <Image Grid.Row="1" Grid.Column="0" Grid.RowSpan="2" Source="icon.png" />
    <Label Grid.Row="1" Grid.Column="1" Text="Main content area goes here..." />
    <Button Grid.Row="2" Grid.Column="1" Text="Action Button" />
</Grid>
                

This Grid defines three rows and two columns. Elements are placed using Grid.Row and Grid.Column attributes.

FlexLayout

FlexLayout is a versatile layout that allows for arranging items in a flexible manner, supporting wrapping, justification, and alignment along primary and secondary axes. It's excellent for responsive UIs.

Example Usage


<FlexLayout Direction="Row" Wrap="Wrap" Justify="SpaceBetween" AlignItems="Center" Padding="10">
    <BoxView Color="Blue" WidthRequest="100" HeightRequest="50" />
    <BoxView Color="Green" WidthRequest="100" HeightRequest="50" />
    <BoxView Color="Red" WidthRequest="100" HeightRequest="50" />
    <BoxView Color="Yellow" WidthRequest="100" HeightRequest="50" />
</FlexLayout>
                

This FlexLayout arranges items horizontally, wraps them to the next line if space is insufficient, and distributes space between items.

Other Layout Controls

Best Practices for Layout

Mastering layout in .NET MAUI is key to building robust and user-friendly cross-platform applications.