我正在使用 BoxView 在我的应用中添加下划线。我有几个非常短的标签 - 诸如"is"或“否”之类的文本。这是带有下划线的 BoxView 的其中一个标签的 XAML:
<StackLayout Orientation="Vertical" Grid.Row="5" Grid.Column="1" Margin="0,4,0,4" HorizontalOptions="Start" BackgroundColor="Purple" MinimumWidthRequest="1">
<Label x:Name="txtUseMetric" TextColor="Blue" FontSize="Small" Text="{Binding UseMetricText}" BackgroundColor="Yellow">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="Value_Tapped" CommandParameter="usemetric" />
</Label.GestureRecognizers>
</Label>
<BoxView BackgroundColor="Green" HeightRequest="1" MinimumWidthRequest="1" />
</StackLayout>
我的问题是 BoxView 的宽度总是超出我的文本我已经尝试覆盖我的 App.Xaml 文件中的 MinWidthRequest,如下所示:
<Style TargetType="BoxView">
<Setter Property="MinimumWidthRequest" Value="3" />
</Style>
但这并没有效果。我已经包含了屏幕截图供您查看。
仅供引用 - 黄色是标签的宽度。您看不到任何紫色(StackLayout 的背景色),因为 StackLayout 和 Label 的宽度相同。第二个屏幕截图显示了如果我删除 BoxView 时屏幕的样子 - 即 Label 和 StackLayout 的大小正确。
关于如何解决这个问题有什么建议吗?
最佳答案
请注意默认HorizontalOptions那Label派生自 View :
默认值为 LayoutOptions。Fill 除非另有说明。
在“Use Metric”标签上添加HorizontalOptions="Start"
:
<Label x:Name="txtUseMetric" TextColor="Blue" FontSize="Small"
Text="{Binding UseMetricText}" BackgroundColor="Yellow"
HorizontalOptions="Start">
<BoxView BackgroundColor="Green" HeightRequest="1"
WidthRequest="{Binding Path=Width, Source={x:Reference txtUseMetric}"
HorizontalOptions="Start"/>
关于xamarin.forms - Xamarin 在使用下划线时形成 BoxView 宽度太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47636459/