Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 42 ยท XAML and Layouts

Flex and Absolute Layouts

Learn FlexLayout (direction, wrap, justify, align, grow, shrink, basis) and AbsoluteLayout (proportional bounds) in .NET MAUI, plus runnable wrapping and proportional layout stand-ins.

FlexLayout and AbsoluteLayout handle the cases stacks and grids do not do well. FlexLayout arranges children in a row or column and can wrap them onto new lines, grow them to fill space, or space them evenly. AbsoluteLayout places children at exact positions or at positions relative to the container. If you know CSS flexbox, FlexLayout will feel familiar: it is modelled on it.

The XAML below is read-only. The two runnable blocks are labelled stand-ins that implement the core arithmetic: a wrapping row, and proportional placement.

FlexLayout

XML
<FlexLayout Direction="Row" Wrap="Wrap"
            JustifyContent="Start" AlignItems="Center" AlignContent="Start">
    <Label Text="HTML"  Margin="4" />
    <Label Text="CSS"   Margin="4" />
    <Label Text="C#"    Margin="4" />
    <Label Text="MAUI"  Margin="4" />
</FlexLayout>

The main properties:

Property Values Meaning
Direction Row, RowReverse, Column, ColumnReverse The main axis
Wrap NoWrap, Wrap, Reverse Whether children move to new lines when they do not fit
JustifyContent Start, Center, End, SpaceBetween, SpaceAround, SpaceEvenly Distribution along the main axis
AlignItems Stretch, Start, Center, End Position on the cross axis (inside a line)
AlignContent same plus space options Distribution of the lines on the cross axis when wrapping

Attached properties on children:

  • FlexLayout.Grow="1" lets the child take a share of free space along the main axis.
  • FlexLayout.Shrink="0" stops it shrinking when space is short.
  • FlexLayout.Basis="100" sets its starting size (a number or a percentage like Basis="50%" through FlexBasis).
  • FlexLayout.Order="-1" changes position without moving it in the XAML.
  • FlexLayout.AlignSelf="End" overrides AlignItems for one child.

A classic use is a tag cloud (wrapping) or a toolbar where one item fills the leftover space:

XML
<FlexLayout Direction="Row" AlignItems="Center">
    <Image Source="logo" HeightRequest="32" />
    <SearchBar FlexLayout.Grow="1" Placeholder="Search" />
    <Button Text="Go" />
</FlexLayout>

The search bar gets all the space the image and button do not use.

How wrapping works, runnable

When Wrap="Wrap" and a child would go past the end of the line, it starts a new line. The stand-in below does exactly that with widths in a 300-unit container, and then spreads leftover space with SpaceBetween. It is a simplified model (no shrink or basis).

C# Stand-in: wrapping and SpaceBetween

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

AbsoluteLayout

AbsoluteLayout places each child in a rectangle: x, y, width, height. The rectangle is set with the attached property AbsoluteLayout.LayoutBounds, and AbsoluteLayout.LayoutFlags says which of the four numbers are proportional (from 0 to 1) rather than device-independent units.

XML
<AbsoluteLayout>
    <BoxView Color="Navy"
             AbsoluteLayout.LayoutBounds="0, 0, 1, 0.3"
             AbsoluteLayout.LayoutFlags="All" />

    <Label Text="Badge"
           AbsoluteLayout.LayoutBounds="1, 0, 80, 30"
           AbsoluteLayout.LayoutFlags="XProportional" />
</AbsoluteLayout>

The first rectangle uses All flags, so every number is proportional: it starts at the top left and covers 100% of the width and 30% of the height. The second uses XProportional: the x value 1 means "aligned to the right edge" (with proportional positions, 1 puts the right edge of the child at the right edge of the container, not at the left edge), while y, width and height are plain units (0, 80, 30).

Available flags: None, XProportional, YProportional, WidthProportional, HeightProportional, PositionProportional, SizeProportional, All. They can be combined with commas: LayoutFlags="XProportional, WidthProportional".

Use AbsoluteLayout for overlays, floating buttons, watermarks and simple designs with layered elements. Avoid it for general screens: it does not adapt to text size or screen shape as well as grids do.

Proportional placement, runnable

The proportional position rule is: left = proportion * (container - childSize). That is why x = 1 aligns the child to the right edge and 0.5 centres it. The stand-in applies this rule.

C# Stand-in: proportional bounds

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Which layout for which job

Need Use
Simple column of controls VerticalStackLayout
Rows and columns, one area fills the rest Grid
Wrapping chips or tags, evenly spaced toolbar FlexLayout
Overlays, floating buttons, layered art AbsoluteLayout

Layouts can be combined: a Grid for the page frame, a FlexLayout for a tag row inside one cell, and an AbsoluteLayout for a floating action button on top.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which FlexLayout property lets children move to a new line when they do not fit?

    Wrap="Wrap" starts a new line when the next child would exceed the main axis size.
  2. 2. What does FlexLayout.Grow="1" on one child do?

    Grow distributes leftover space among children that have a grow value.
  3. 3. In an AbsoluteLayout with XProportional, what does x = 1 mean?

    Proportional position is x * (containerWidth - childWidth), so 1 aligns the child to the right edge.
  4. 4. Which layout suits a floating action button over page content?

    AbsoluteLayout can overlay and position a child exactly, for example bottom right.

Summary

  • FlexLayout arranges children along an axis with wrapping, justification, grow and shrink.
  • AbsoluteLayout positions children with LayoutBounds and LayoutFlags; proportional positions use x * (container - size).
  • Pick the simplest layout that fits and combine layouts when needed.

Next: Sizing, Alignment and Spacing