【问题标题】:Xamarin forms vertically center text over imageXamarin 在图像上形成垂直居中的文本
【发布时间】:2018-01-28 06:11:06
【问题描述】:

我正在学习 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 属性设置为标签,但这样一来,标签将仅以我正在测试的设备为中心。其他更小或更大的设备,标签可能(并且可能)放置在错误的位置。

有什么帮助吗?

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    您不需要在 Grid 中使用 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>
    

    【讨论】:

    • 根据JDalri的描述,Horizo​​ntalTextAlignment应该是Start。
    • 你说得对@ColeXia,我没有注意到这一点。现在我已经编辑了答案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 2013-10-15
    • 2015-04-26
    • 2010-12-30
    相关资源
    最近更新 更多