Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Is there a way to add a View to a RelativeLayout so that it is centered but without settings bounds?

$
0
0

I have the following logic for centering a view in the middle of a layout (which creates a background overlay in this case).

backdrop.Children.Add(popup,
    Constraint.RelativeToParent(p => (Width / 2) - (popup.WidthRequest / 2)),
    Constraint.RelativeToParent(p => (Height / 2) - (popup.HeightRequest / 2)),
    Constraint.RelativeToParent(p => popup.WidthRequest),
    Constraint.RelativeToParent(p => popup.HeightRequest));

The problem here is that I'm required to specify the dimentions of the popup view in order to calculate its position. What I'd like to do is have its dimentions calculated automatically, based on the content. I could do the following

backdrop.Children.Add(popup, Constraint.Constant(0), Constraint.Constant(0));

but then the view isn't centered on the screen.

Is there a way to center it, without using the dimentions for the calculation? Or is there another solution that would allow centering based on the automatically calculated dimentions of the popup view?


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>