简体   繁体   English

Xamarin.Forms:如何在行中居中放置多重标签?

[英]Xamarin.Forms: how to center a multine label in a Row?

I try to vertically center a multiline Label that contains a text that can be displayed on 1 or 2 lines. 我尝试使多行标签垂直居中,该多行标签包含可以显示为1或2行的文本。

For the moment, I'm not able to get the expected rendering... 目前,我无法获得预期的渲染...

I can have the multiline label that automatically extends but it is top aligned and not centered: 我可以使用自动扩展的多行标签,但它是顶部对齐的而不是居中的: 标签未居中

This is the attached XAML: 这是附件的XAML:

<Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="0" />
</Grid.RowDefinitions>

<local:MultiLineLabel
    Grid.Row="0"
    Grid.Column="1"
    BackgroundColor="Orange"
    Text="{ Binding encart_titre }"         
    VerticalTextAlignment="Center"
    LineBreakMode="TailTruncation"
    Lines="2"
    ...
/>

I can also center the label, but in this case it doesn't auto extends: 我也可以将标签居中,但是在这种情况下,它不会自动扩展: 在此处输入图片说明

This is the attached XAML: 这是附件的XAML:

<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="0" />
</Grid.RowDefinitions>

<local:MultiLineLabel
    Grid.Row="0"
    Grid.Column="1"
    BackgroundColor="Orange"
    Text="{ Binding encart_titre }"         
    VerticalTextAlignment="Center"
    LineBreakMode="TailTruncation"
    Lines="2"
    VerticalOptions="FillAndExpand"
    ...
/>

Would you have any explanation? 你有什么解释吗? I also tried to add StackLayound around the Label, but this didn't change anything... 我还尝试在Label周围添加StackLayound,但这并没有改变任何内容。

The first choice for centering a control would be to use VerticalOptions="CenterAndExpand" , have you tried that instead of VerticalOptions="FillAndExpand" ? 使控件居中的首选是使用VerticalOptions="CenterAndExpand" ,您是否尝试过使用它而不是VerticalOptions="FillAndExpand" Here's more information about the LayoutOptions in Xamarin.Forms. 这是有关Xamarin.Forms中LayoutOptions 的更多信息

So, this would be the correct way to center vertically: 因此,这将是垂直居中的正确方法:

<local:MultiLineLabel
    Grid.Row="0"
    Grid.Column="1"
    BackgroundColor="Orange"
    Text="{ Binding encart_titre }"         
    VerticalTextAlignment="Center"
    LineBreakMode="TailTruncation"
    Lines="2"
    VerticalOptions="CenterAndExpand" <!-- This one here -->
    ...
/>

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

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