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

前端 未结 7 1782
谎友^
谎友^ 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:35

    In code you can try this:

    RelativeLayout relativeLayout = new RelativeLayout ();
    StackLayout stack = new StackLayout ();
    ActivityIndicator indicator = new ActivityIndicator ();
    
    relativeLayout.Children.Add (stack);
    relativeLayout.Children.Add (indicator);
    
    RelativeLayout.SetBoundsConstraint (stack, () => relativeLayout.Bounds);
    
    RelativeLayout.SetBoundsConstraint (indicator,
        BoundsConstraint.FromExpression (() =>  
            new Rectangle (relativeLayout.Width / 2 - 16, relativeLayout.Height / 2 - 16, 32, 32)));
    

    Assuming width/height of the ActivityIndicator of 32/32, you can pick a size that fits your needs or calculate the size on the fly.

    There still seem to be bugs in RelateiveLayout, it throws unexpected null pointer exceptions on occasion, even for solvable constraints

提交回复
热议问题