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

达利

我正在学习Xamarin,我想将图像显示为背景,并在该图像上添加一些文字...

就这样,我设法完成了,但是文本显示在图像的左上角。

我希望将文本放置在图像的垂直中心,但要保留在图像的左侧。

到目前为止,我的代码是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>

我已经尝试过verticalOptions之类的,但是没有效果。

这种工作的一件事是在标签上设置了Maring属性,但是那样,标签将仅居中放置在我正在测试的设备上。标签可能会(可能会被)放置在错误的地方,无论其他大小的标签。

有什么帮助吗?

迭戈·拉斐尔·苏扎

您在网格内部不需要RelativeLayout。除了让您的代码更清晰之外,Grid本身已经能够处理视图覆盖。

它可能起作用:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章