【问题标题】:How to set the width of one element to the width of another in Xamarin?如何在 Xamarin 中将一个元素的宽度设置为另一个元素的宽度?
【发布时间】:2019-12-05 18:47:04
【问题描述】:

我正在使用框视图在 Xamarin 中为某些文本添加下划线,并且我想将框视图的宽度设置为标签的宽度

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    标签有一个属性TextDecorations。您可以将其设置为 Underline 以在不使用 BoxView 的情况下为文本添加下划线。

        <Label Text="Your Text" TextDecorations="Underline">
    

    【讨论】:

    • 为什么这不是答案?如果您想简单地为文本添加下划线,请使用TextDecorations。接受的答案确实正确回答了问题,但这不是给文本加下划线的最佳方式。
    • @ottermatic 因为我需要下划线与文本颜色不同
    【解决方案2】:

    为 Label 定义 WidthRequest 并将其绑定到 Box 视图,如下所示。

    <StackLayout Padding="0,10" HorizontalOptions="Center" VerticalOptions="Center" Orientation="Horizontal">
        <Button x:Name="BtnSend" Clicked="BtnSend_Clicked" Text="Send" WidthRequest="150" />
    
        <Label Text="Hello" BackgroundColor="Yellow" WidthRequest="{Binding WidthRequest, Source={x:Reference BtnSend}}"  />
    </StackLayout>
    

    【讨论】:

      猜你喜欢
      • 2019-06-18
      • 2018-01-29
      • 2020-02-15
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      • 2011-02-13
      • 2018-10-15
      • 2020-08-10
      相关资源
      最近更新 更多