Xamarin Forms - 内容没有在 stackLayout 中垂直居中对齐 [英] Xamarin Forms - Content is not getting vertically center aligned in the stackLayout

查看:24
本文介绍了Xamarin Forms - 内容没有在 stackLayout 中垂直居中对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a custom control in Xamarin forms to show the alert message.

在少数情况下,内容会垂直居中对齐,但在少数情况下会溢出框架.

On few scenarios the content is getting vertically aligned middle but on few cases it is overflowing the frame.

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

Could you please let me know what changes I need to make to have the content always vertically middle and size of the content expands.

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

推荐答案

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

You should adjust the vertical options of your label, either to Center, or to CenterAndExpand:

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

这篇关于Xamarin Forms - 内容没有在 stackLayout 中垂直居中对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆