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:
- Measure: Each element is measured to determine its desired size.
- 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
RelativeLayout
: Arranges elements relative to siblings or the parent.AbsoluteLayout
: Arranges elements at specific coordinates. Use with caution as it can reduce responsiveness.ScrollView
: Allows content that exceeds the screen dimensions to be scrollable.Frame
: Displays its content within a frame with a border and background.Border
: Adds a border around its child element.ContentView
: A container that can hold a single child element, often used for custom controls.
Best Practices for Layout
- Prefer flexible layouts: Use
Grid
andFlexLayout
for responsive designs. - Avoid deep nesting: Excessive nesting can impact performance.
- Understand spacing and padding: Use
Spacing
andPadding
properties effectively to create visually appealing UIs. - Use appropriate units: MAUI uses device-independent units for measurements, ensuring consistency across devices.
Mastering layout in .NET MAUI is key to building robust and user-friendly cross-platform applications.