Xamarin.Forms:如何在一个行中居中多标签?
问题描述:
我尝试垂直居中包含文本的multiline Label,该文本可以显示在1行或2行上。Xamarin.Forms:如何在一个行中居中多标签?
就目前而言,我无法得到预期的渲染...
这是附加的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"
...
/>
这是附加的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 -->
...
/>
谢谢它与'VerticalOptions =“CenterAndExpand”' –
只要稍微规范:标签必须嵌入一个'StackLayout' 'VerticalOptions =“CenterAndExpand”'和'HorizontalOptions =“CenterAndExpand”'。 –