Xaml 如何使按钮展开以Xamarin.Forms显示其文本
我为我的按钮创建了Xaml 如何使按钮展开以Xamarin.Forms显示其文本,xaml,xamarin.forms,Xaml,Xamarin.forms,我为我的按钮创建了样式,但当我使用较长的文本时,它们会被截断 这就是风格: <Style x:Key="DefaultButton" TargetType="Button"> <!--<Setter Property="WidthRequest"> <Setter.Value> <OnIdiom x:TypeArgum
样式
,但当我使用较长的文本时,它们会被截断
这就是风格:
<Style x:Key="DefaultButton" TargetType="Button">
<!--<Setter Property="WidthRequest">
<Setter.Value>
<OnIdiom x:TypeArguments="x:Double"
Phone="150"
Tablet="200" />
</Setter.Value>
</Setter>
<Setter Property="HeightRequest">
<Setter.Value>
<OnIdiom x:TypeArguments="x:Double"
Phone="70"
Tablet="100" />
</Setter.Value>
</Setter>-->
<Setter Property="BackgroundColor" Value="{StaticResource BaseColor}" />
<Setter Property="TextColor" Value="White" />
<Setter Property="HorizontalOptions" Value="CenterAndExpand" />
<Setter Property="VerticalOptions" Value="FillAndExpand" />
<Setter Property="Margin" Value="5" />
<Setter Property="FontSize">
<Setter.Value>
<OnIdiom x:TypeArguments="x:Double"
Phone="20"
Tablet="25" />
</Setter.Value>
</Setter>
</Style>
按钮:
<Button Grid.Row="1"
Grid.Column="1"
Style="{DynamicResource DefaultButton}"
Text="Basic button with long text" />
下面是文本较长的按钮的外观:
我可以为按钮设置一个非常大的HeightRequest
,但这是非常糟糕的做法
对此我应该怎么做?您可以使用
LineBreakMode
属性。以下是帮助您选择最合适模式的文档
在这种情况下,我经常使用
网格
和框视图
和标签
组成按钮,然后将手势识别器
放在网格
上。如果需要,将所有这些添加到自定义控件以便于重用
我使用内存执行了下面的手势识别器
,因此它可能需要一些修复:
<Grid x:Name="BasicButtonGrid"
VerticalOptions="End">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.MinimumHeightRequest>
<OnIdiom x:TypeArguments="x:Double"
Phone="40"
Tablet="70"/>
</Grid.MinimumHeightRequest>
<Grid.GestureRecognizers>
<TapGestureRecognizer Tapped="OnBasicButtonTapped"/>
</Grid.GestureRecognizers>
<BoxView BackgroundColor="Blue"
VerticalOptions="EndAndExpand"
InputTransparent="True"
Grid.Row="0"
Grid.Column="0"/>
<Label Text="Basic Button with long text"
TextColor="White"
FontSize="Medium"
Grid.Row="0"
Grid.Column="0"/>
</Grid>
您可以忽略设置高度请求,让按钮自行调整大小。你有什么理由要明确地设置它吗?@WilliamCorncodecker你可能误解了我。我不想显式地设置HeigthRequest(它被注释掉),我希望它是自动调整大小的。我该怎么做?四年后,XF 4.6.0.726出现了同样的问题Button类中没有LineBreakMode
属性。