I have a label inside of a stack layout. It has its HorizontalTextAlignment
set to TextAlignment.Center
. It works correctly on the initial loading of the page, and when a new value is selected - however when the page is left and then returned to, it loses its alignment. This is only a problem on Android and not on iOS. I hope the image below can help illustrate my point. This is the only code that affects the Label's alignment, other than that it's just changing it's text through a binding, but I don't see how that would change it's alignment. Any ideas? Thanks.
StackLayout durationLayout = new StackLayout {
Orientation = StackOrientation.Vertical,
WidthRequest = (App.ScreenDpWidth / 3) - (GMStyle.Margin.PageMargin.Left * 2),
VerticalOptions = LayoutOptions.Center,
Margin = new Thickness(0, 5, 0, 0),
HorizontalOptions = LayoutOptions.Start
};
durationLabel = new Label {
Style = (Style)Application.Current.Resources["DurationLabelStyle"],
HorizontalTextAlignment = TextAlignment.Center,
//VerticalTextAlignment = TextAlignment.Center,
};
durationLabel.SetBinding(Label.TextProperty, "DurationDisplay");
durationLayout.Children.Add(timeSpanPicker);
durationLayout.Children.Add(durationLabel);
durationLayout.GestureRecognizers.Add(timeSpanTap);
This is a bug in Xamarin Forms, that I believe appeared in 2.3.3 or 2.3.4, it works fine in 2.3.2. I don't know of any bug report lodged against it just yet, this is the closest I could find, that may be related.
https://bugzilla.xamarin.com/show_bug.cgi?id=49311
As a workaround, you can use HorizontalOptions=LayoutOptions.CenterAndExpand
instead of HorizontalTextAlignment
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With