MAUI Layouts: Arranging Your UI Elements

In .NET MAUI, layouts are fundamental to how you structure and organize the user interface of your applications. They provide a way to position and size controls within a parent container. Understanding different layout containers is key to creating responsive and visually appealing applications that adapt to various screen sizes and orientations.

Common Layout Containers

MAUI offers several built-in layout containers, each with its own purpose and behavior. Let's explore the most common ones:

1. StackLayout

The StackLayout arranges its children in a one-dimensional line, either horizontally or vertically. It's ideal for simple, linear arrangements of elements.

Properties:

<VerticalStackLayout Spacing="10" Padding="10">
    <Label Text="Welcome to MAUI!" FontSize="24"/>
    <Button Text="Click Me"/>
    <Image Source="dotnet_bot.png"/>
</VerticalStackLayout>
Use HorizontalStackLayout for horizontal arrangements.

2. Grid

The Grid is a powerful and flexible layout container that arranges its children in rows and columns. It's perfect for complex UIs where precise alignment is needed.

Properties:

You can specify the size of rows and columns using absolute values, percentages, or the Auto keyword.

<Grid RowDefinitions="Auto,*,Auto" ColumnDefinitions="*,Auto" Padding="10">
    <Label Grid.Row="0" Grid.Column="0" Text="Header" BackgroundColor="LightGray"/>
    <Label Grid.Row="1" Grid.Column="0" Text="Main Content Area"/>
    <Button Grid.Row="2" Grid.Column="0" Text="Action Button"/>
    <Image Grid.Row="1" Grid.Column="1" Source="info_icon.png" WidthRequest="50" HeightRequest="50"/>
</Grid>
The * unit in row/column definitions represents a proportional size.

3. AbsoluteLayout

AbsoluteLayout positions its children at specific coordinates (X, Y) within the layout. This gives you pixel-perfect control but can make your UI less responsive.

Properties:

<AbsoluteLayout>
    <Image Source="background.jpg" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All"/>
    <Label Text="Overlay Text" AbsoluteLayout.LayoutBounds="50, 100, -1, -1"/>
</AbsoluteLayout>

4. RelativeLayout

RelativeLayout positions its children relative to the layout itself or to other sibling elements. This offers a good balance between control and responsiveness.

Properties:

<RelativeLayout Padding="10">
    <Button Text="Top Left"
            RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=X}"
            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y}"/>
    <Button Text="Bottom Right"
            RelativeLayout.XConstraint="{ConstraintExpression Type=RelativeToParent, Property=XConstraint, Element=Self, Factor=1, Offset=-50}"
            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=YConstraint, Element=Self, Factor=1, Offset=-50}"/>
</RelativeLayout>

Best Practices for Layouts

Example: A Simple App Layout

Here's a common pattern using a Grid as the main layout, with a header, content area, and footer.

<Grid RowDefinitions="Auto,*,Auto">
    <Border Grid.Row="0" BackgroundColor="DarkBlue" Padding="15">
        <Label Text="My MAUI App Header" TextColor="White" FontSize="20" HorizontalOptions="Center"/>
    </Border>

    <ScrollView Grid.Row="1" Padding="20">
        <!-- Your main content controls go here -->
        <StackLayout>
            <Label Text="Welcome to the main content!" FontSize="18" Margin="0,0,0,15"/>
            <Entry Placeholder="Enter text"/>
            <Button Text="Submit"/>
        </StackLayout>
    </ScrollView>

    <Label Grid.Row="2" Text="© 2023 MyCompany" TextColor="Gray" HorizontalOptions="Center" Padding="10"/>
</Grid>

By mastering these layout containers and best practices, you can build robust and adaptable user interfaces for your .NET MAUI applications.