Xamarin Forms - 在堆栈布局中居中标题 [英] Xamarin Forms - center title in a stacklayout

查看:39
本文介绍了Xamarin Forms - 在堆栈布局中居中标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的屏幕截图显示了一个包含汉堡菜单和标题的标题.请注意标题如何以其边界区域(红色框)为中心.如何让标题以手机宽度为中心,但将汉堡菜单保留在原处?

The screenshot below shows a header that includes a hamburger menu and a title. Notice how the title is centered on it's bounding area (the red box). How can I get the title to center on the width of the phone, but leave the hamburger menu where it is?

这里是创建头部区域的代码...

Here is the code that creates the header area...

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="WP.MobileMidstream.Device.Pages.RunSheetHeader">
    <ContentView.Resources>
        <Style x:Key="HeaderStyle" TargetType="StackLayout">
            <Setter Property="BackgroundColor" Value="#00458C" />
        </Style>
    </ContentView.Resources>
    <ContentView.Content>
        <StackLayout Orientation="Horizontal"
                     HorizontalOptions="FillAndExpand"
                     Style="{StaticResource HeaderStyle}">
            <StackLayout.HeightRequest>
                <OnPlatform x:TypeArguments="x:Double">
                    <On Platform="iOS">80</On>
                    <On Platform="Android">56</On>
                </OnPlatform>
            </StackLayout.HeightRequest>
            <Image Source="hamburger_icon" 
                   Margin="10" />
            <Label VerticalTextAlignment="Center"
                   HorizontalTextAlignment="Center"
                   HorizontalOptions="FillAndExpand"
                   FontSize="20"
                   BackgroundColor="Red"
                   TextColor="White">Daily Run Sheet</Label>
        </StackLayout>
    </ContentView.Content>
</ContentView>

推荐答案

@Jason 建议使用 Grid 而不是 StackLayout.这是我想出的作品.谢谢@Jason!

@Jason suggested a Grid instead of the StackLayout. Here is what I came up with that works. Thanks @Jason!

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="WP.MobileMidstream.Device.Pages.RunSheetHeader">
    <ContentView.Resources>
        <Style x:Key="HeaderStyle" TargetType="Grid">
            <Setter Property="BackgroundColor" Value="#00458C" />
        </Style>
    </ContentView.Resources>
    <ContentView.Content>
        <Grid Style="{StaticResource HeaderStyle}">
            <Grid.RowDefinitions>
                <RowDefinition>
                    <RowDefinition.Height>
                        <OnPlatform x:TypeArguments="GridLength">
                            <On Platform="iOS">80</On>
                            <On Platform="Android">56</On>
                        </OnPlatform>
                    </RowDefinition.Height>
                </RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Label Grid.Row="0"
                   Grid.Column="0"
                   Grid.ColumnSpan="2"
                   VerticalTextAlignment="Center"
                   HorizontalTextAlignment="Center"
                   HorizontalOptions="Fill"
                   FontSize="20"
                   TextColor="White">Daily Run Sheet</Label>
            <Image Source="hamburger_icon" 
                   Grid.Row="0"
                   Grid.Column="0"
                   Margin="10" />
        </Grid>
    </ContentView.Content>
</ContentView>

这篇关于Xamarin Forms - 在堆栈布局中居中标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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