Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Label losing text alignment when page returned to

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);

enter image description here

like image 842
Will Nasby Avatar asked Mar 31 '17 19:03

Will Nasby


1 Answers

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.

like image 133
Adam Avatar answered Sep 20 '22 13:09

Adam