xamarin.forms - Xamarin 在使用下划线时形成 BoxView 宽度太长

标签 xamarin.forms label width underline stacklayout

我正在使用 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 的大小正确。

关于如何解决这个问题有什么建议吗?

BoxView 太长的屏幕截图使标签和 StackLayout 太长 Screen shot with BoxView Too Long

已删除 BoxView 并正确调整标签和堆栈布局大小的屏幕截图 enter image description here

最佳答案

请注意默认HorizontalOptionsLabel派生自 View :

默认值为 LayoutOptions。Fill 除非另有说明。

在“Use Metric”标签上添加Horizo​​ntalOptions="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/

相关文章:

c# - 在代码隐藏中设置 Xamarin.Forms 绑定(bind) CommandParameter

xamarin - 从 Visual Studio 2017 15.5.1 开始,如何创建 Xamarin.Forms 项目以使用 PCL 来定位最低 Android KitKat (API 19)?

android - 如何在 MasterDetailPage - Xamarin.Forms 中自定义箭头图标、页面图标和页面标题

r - 有没有办法手动更改使用 rms 包绘制的列线图中的标签颜色?

css - DIV 宽度未扩展以适应子 DIV 的宽度 - IE7

css - javaFX宽度css没有反应?

android - Xamarin.Forms Android 沉浸式模式,带软键盘或警报

ios - 如何修复约束不显示重复的单词?

swift - 文本字段 - 标签 - 按钮 根据输入回答

css - 将具有百分比 (%) 宽度的 div 设置为其最大内容的最小宽度