Xamarin.Forms - How to overlay an ActivityIndicator in the middle of a StackLayout programmatically

前端 未结 7 1756
谎友^
谎友^ 2020-12-13 06:59

I want to overlay my ActivityIndicator in the middle of my form but I\'m not entirely sure how I can go about this, I assume I need to wrap my stack layout in a relative lay

7条回答
  •  囚心锁ツ
    2020-12-13 07:37

    If you have problems with RelativeLayout, you can also use AbsoluteLayout which works in a similar context. Sample code below:

    var overlay = new AbsoluteLayout();
    var content = new StackLayout();
    var loadingIndicator = new ActivityIndicator();
    AbsoluteLayout.SetLayoutFlags(content, AbsoluteLayoutFlags.PositionProportional);
    AbsoluteLayout.SetLayoutBounds(content, new Rectangle(0f, 0f, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
    AbsoluteLayout.SetLayoutFlags(loadingIndicator, AbsoluteLayoutFlags.PositionProportional);
    AbsoluteLayout.SetLayoutBounds(loadingIndicator, new Rectangle(0.5, 0.5, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
    overlay.Children.Add(content);
    overlay.Children.Add(loadingIndicator);
    

    If you would like a full working example, I have made one available @ https://github.com/teamtam/xamarin-forms-timesheet

提交回复
热议问题