我正在学习 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 属性,但这样一来,标签将仅以我正在测试的设备为中心。其他设备,更小或更大,标签可能(并且可能会)被放置在错误的位置。
有什么帮助吗?
最佳答案
您不需要 Grid 内的 RelativeLayout。除了让您的代码更清晰之外,网格本身已经能够处理 View 覆盖。
它可能有效:
<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>
关于xaml - Xamarin 在图像上形成垂直居中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45775580/