简体   繁体   English

Xamarin在图像上形成垂直居中文本

[英]Xamarin forms vertically center text over image

I'm learning Xamarin and I want to display an image as background with some texto over this image... 我正在学习Xamarin,我想将图像显示为背景,并在该图像上添加一些文字...

Just that, I manage to acomplish, but the text is displayed at the top left corner of the image. 就这样,我设法完成了,但是文本显示在图像的左上角。

I'd like the text to be placed at the vertical center of the image, but stick to the left side of it. 我希望将文本放置在图像的垂直中心,但要保留在图像的左侧。

My code so far XAML: 到目前为止,我的代码是XAML:

<Grid RowSpacing="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="20*" ></RowDefinition>
            <RowDefinition Height="65*"></RowDefinition>
            <RowDefinition Height="15*"></RowDefinition>
        </Grid.RowDefinitions>

        <RelativeLayout Grid.Row="0">
            <Image Source="login.png" Aspect="Fill" />
            <Label Text="Bem vindo" TextColor="White" FontSize="22"></Label>
        </RelativeLayout>

        <Label Grid.Row="1" Text="linha 2" BackgroundColor="Coral"></Label>
        <Button Grid.Row="2" Text="linha 3" BackgroundColor="DarkRed"></Button>
    </Grid>

I've tried verticalOptions and such, but with no effect. 我已经尝试过verticalOptions之类的,但是没有效果。

One thing that kind of worked is settin a Maring property to the label, but that way, the label would be centered only to the device I'm testing on. 这种工作的一件事是在标签上设置了Maring属性,但是那样,标签将仅居中放置在我正在测试的设备上。 Other devices, smaller or greater, the label could (and probably would) be placed at the wrong place. 其他设备,无论大小,标签可能会(而且可能会)放置在错误的位置。

Any help? 有什么帮助吗?

You don't need a RelativeLayout inside the Grid. 您在网格内部不需要RelativeLayout。 The Grid itself already is able to handle views overlay besides let your code clearer. 除了让您的代码更清晰之外,Grid本身已经能够处理视图覆盖。

It may works: 它可能起作用:

<Grid RowSpacing="0">
    <Grid.RowDefinitions>
        <RowDefinition Height="20*"></RowDefinition>
        <RowDefinition Height="65*"></RowDefinition>
        <RowDefinition Height="15*"></RowDefinition>
    </Grid.RowDefinitions>

    <Image Grid.Row="0" Grid.Column="0"
           Source="login.png"
           HorizontalOptions="FillAndExpand"
           VerticalOptions="FillAndExpand" 
           Aspect="Fill" />
    <Label Grid.Row="0" Grid.Column="0"
           Text="Bem vindo" 
           HorizontalOptions="FillAndExpand"
           VerticalOptions="FillAndExpand"
           HorizontalTextAlignment="Start"
           VerticalTextAlignment="Center"
           TextColor="White" 
           FontSize="22"/>

    <Label Grid.Row="1" 
           Text="linha 2" 
           BackgroundColor="Coral"/>
    <Button Grid.Row="2" 
            Text="linha 3" 
            BackgroundColor="DarkRed"/>
</Grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM