Xamarin表单-内容在stackLayout中未垂直居中对齐

时间:2018-10-10 14:28:31

标签: xamarin.forms

我正在尝试在Xamarin表单中创建一个自定义控件以显示警报消息。

在某些情况下,内容在中间垂直对齐,但在少数情况下,内容会溢出框架。

您能否让我知道我需要进行哪些更改才能使内容始终垂直居中并且内容的大小会扩大。

        _alertLayout = new StackLayout
        {
            Orientation = StackOrientation.Horizontal,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand
        };

        _alertLayoutHighlighter = new StackLayout
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            BackgroundColor = Constants.MMSGAlertBackgroundColor,
            Padding = 0,
            WidthRequest = 8
        };

        _alertBoxView = new BoxView
        {
            Color = Constants.MMSGAlertTextColor,
            VerticalOptions = LayoutOptions.FillAndExpand
        };

        _alertLayoutHighlighter.Children.Add(_alertBoxView);

        _alertLayoutContent = new StackLayout
        {
            Orientation = StackOrientation.Vertical,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            Padding = new Thickness(16)
        };

        _alertMessage = new Label
        {
            TextColor = Constants.MMSGAlertTextColor,
            FontSize = 12
        };

        _alertLayoutContent.Children.Add(_alertMessage);

        _alertLayout.Children.Add(_alertLayoutHighlighter);
        _alertLayout.Children.Add(_alertLayoutContent);

        _frame = new Frame
        {
            OutlineColor = Constants.MMSGAlertBorderColor,
            BackgroundColor = Constants.MMSGAlertBackgroundColor,
            Padding = new Thickness(2, 2, 0, 2),
            HasShadow = false,
            Content = _alertLayout,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand
        };

        this.Content = _frame;

enter image description here

1 个答案:

答案 0 :(得分:0)

您应该将标签的垂直选项调整为“居中”或“ CenterAndExpand”:

hg convert <old_repo> <new_repo>