Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding space in Xamarin.Forms layouts?

What's the proposed way to add space to layouts in Xamarin.Forms?

One way would be to add a Frame with no children like so:

new Frame {
    BackgroundColor = Color.White,
    HeightRequest = 1,
    MinimumHeightRequest = 1,
    HasShadow = false
}

Unfortunately, HeightRequest and MinimumHeightRequest get ignored.

Does a better way exist?

like image 309
SteAp Avatar asked Jun 08 '14 01:06

SteAp


1 Answers

What I do worked perfectly for me:

Suppose you want to distribute 2 Labels evenly on a horizontal StackLayout:

new StackLayout
{
    Orientation = StackOrientation.Horizontal,
    HorizontalOptions=LayoutOptions.CenterAndExpand,
    Spacing = 0, // <- Very important!!
    Children = {
        new Label { Text = "Label 1" },
        new BoxView { HorizontalOptions = LayoutOptions.FillAndExpand }, // <- the clever part
        new Label { Text = "Label 2" }
    }
};

Summary

By inserting BoxViews that fill the remaining space ("FillAndExpand") between your views, your views appear evenly distributed. By setting Spacing = 0, you don't get extra space between your views.

like image 159
Lay González Avatar answered Sep 18 '22 12:09

Lay González