Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center nested content in Xamarin Forms StackLayout?

I expect this sample code to create 3 nested boxes with each box centered within its parent. My understanding is based on What is the difference between Xamarin.Form's LayoutOptions, especially Fill and Expand?. The boxes seem to be centered horizontally but not vertically.

Full repo: https://github.com/pawelpabich/Xamarin-Forms-Nested-Content

Sample Code:

var mainPage = new ContentPage();

        var content = new StackLayout()
        {
            VerticalOptions = LayoutOptions.Center,
            HorizontalOptions = LayoutOptions.Center,
            BackgroundColor = Color.Yellow,
            HeightRequest = 200,
            WidthRequest = 200,
            Children =
            {
                new StackLayout()
                {
                    BackgroundColor = Color.Blue,
                    HeightRequest = 100,
                    WidthRequest = 100,
                    VerticalOptions = LayoutOptions.Center,
                    HorizontalOptions = LayoutOptions.Center,
                    Children =
                    {
                        new StackLayout()
                        {
                            BackgroundColor = Color.Green,
                            HeightRequest = 50,
                            WidthRequest = 50,
                            VerticalOptions = LayoutOptions.Center,
                            HorizontalOptions = LayoutOptions.Center,
                        }
                    }
                }
            }

        };

        mainPage.Content = content;
        MainPage = mainPage;

End result when the app is running on a windows emulator: enter image description here.

like image 659
Pawel Pabich Avatar asked Apr 07 '15 04:04

Pawel Pabich


People also ask

How do I use StackLayout in xamarin forms?

By default, a StackLayout is oriented vertically. In addition, a StackLayout can be used as a parent layout that contains other child layouts. The StackLayout class defines the following properties: Orientation , of type StackOrientation , represents the direction in which child views are positioned.

How do you set the margins in code behind in xamarin forms?

You can set Thickness in XAML by setting one, two or four values separated by comma or whitespace: "1 2 3 4" is same as "1, 2, 3, 4" and sets: 1 for Left.

What is stack layout?

A StackLayout is a layout that organizes its children in a one-dimensional stack, either horizontally or vertically. By default, a StackLayout is oriented vertically. Visual Studio.


1 Answers

As explained here in great detail:

Expansion: Will the element occupy more space if available?

  • Suffix Expand: If the parent view is larger than the combined size of all its children, i.e. additional space is available, then the space is proportioned amongst child views with that suffix. Those children will "occupy" their space, but do not necessarily "fill" it. We'll have a look on this behavior in the example below.

  • No suffix: The children without Expand suffix won't get additional space, even if more space is available.

        var mainPage = new ContentPage();

        var content = new StackLayout()
        {
            VerticalOptions = LayoutOptions.Center,
            HorizontalOptions = LayoutOptions.Center,
            BackgroundColor = Color.Yellow,
            HeightRequest = 200,
            WidthRequest = 200,
            Children =
            {
                new StackLayout()
                {
                    BackgroundColor = Color.Blue,
                    HeightRequest = 100,
                    WidthRequest = 100,
                    VerticalOptions = LayoutOptions.CenterAndExpand,
                    HorizontalOptions = LayoutOptions.Center,
                    Children =
                    {
                        new StackLayout()
                        {
                            BackgroundColor = Color.Green,
                            HeightRequest = 50,
                            WidthRequest = 50,
                            VerticalOptions = LayoutOptions.CenterAndExpand,
                            HorizontalOptions = LayoutOptions.Center,
                        }
                    }
                    }
            }

            };

        mainPage.Content = content;
        MainPage = mainPage; 

Works fine.

So, when you use CenterAndExpand instead of Center, your inner items can occupy more space than they “need” (you are forcing StackLayout to give more space). Otherwise StackLayout will give them only “enough” space but not more as this is it's default behaviour.

like image 113
Artur Shamsutdinov Avatar answered Sep 28 '22 08:09

Artur Shamsutdinov