Xamarin.Forms:如何在一个行中居中多标签?

问题描述:

我尝试垂直居中包含文本的multiline Label,该文本可以显示在1行或2行上。Xamarin.Forms:如何在一个行中居中多标签?

就目前而言,我无法得到预期的渲染...

我可以有自动延长多标签,但它是顶部对齐,而不是居中: label is not centered

这是附加的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" 
    ... 
/> 

我也可以居中的标签,但在这种情况下,它不会自动扩展: enter image description here

这是附加的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" 
    ... 
/> 

你有什么解释?我也尝试添加StackLayound周围的标签,但这并没有改变什么...

为中心控制的第一选择是使用VerticalOptions="CenterAndExpand",你有没有试过,而不是VerticalOptions="FillAndExpand"?关于Xamarin.Forms中的LayoutOptions的Here's more information

所以,这将是垂直居中的正确方法:

<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 --> 
    ... 
/> 
+0

谢谢它与'VerticalOptions =“CenterAndExpand”' –

+1

只要稍微规范:标签必须嵌入一个'StackLayout' 'VerticalOptions =“CenterAndExpand”'和'Horizo​​ntalOptions =“CenterAndExpand”'。 –