【问题标题】:Xamarin.Forms Button Padding Cuts Off FontIconXamarin.Forms 按钮填充切断 FontIcon
【发布时间】:2020-05-21 23:40:15
【问题描述】:

我有一个使用来自Material Design Icons 的字体图标的按钮。 (显示此图标以及其他几个图标都可以正常工作,因此为简洁起见省略了转换器)。这是按钮的 XAML:

<Button Margin="10"
        Padding="0"
        BackgroundColor="{StaticResource Background}"
        CornerRadius="25"
        FontFamily="{StaticResource MaterialFontFamily}"
        FontSize="36"
        HeightRequest="50"
        HorizontalOptions="EndAndExpand"
        Text="{Binding PlusIcon, Converter={StaticResource NameToIcon}}"
        WidthRequest="50" />

如您所见,我已将填充设置为 0,但仍显示如下:

只是为了测试一下,我也使用常规文本完成了此操作:

如果我将字体大小设置得更小看起来不错,但这不是我想要的:

如何强制移除填充?当我更改字体大小时,为什么文本没有居中?问题发生在 IOS 和 Android 上。

【问题讨论】:

  • 我不太了解这些材质图标是如何工作的,但如果它们像 FontAwesome 那样工作,您是否尝试过在这里使用标签?
  • 这个问题似乎是由自定义字体引起的(可能自定义图标默认有填充)。因为它在我这边工作得很好(我使用了默认字体)。
  • @FreakyAli 是的,与 FontAwesome 完全相同。我还没有尝试过标签,我会试一试。
  • @LucasZhang-MSFT 我的问题中显示带有常规文本(无自定义字体)症状的图像表明这是不正确的。
  • 使用标签对你有用吗?

标签: c# xaml button xamarin.forms padding


【解决方案1】:

您应该使用ImageButtonFontImageSource

首先,您在样式中声明 FontImageSource:

<FontImageSource x:Key="IconBikeSmall"
                 FontFamily="{StaticResource FontAwesome}"
                 Glyph="{x:Static styles:IconFont.Biking}"
                 Size="{StaticResource SizeSmallIcon}"
                 Color="{StaticResource SecondaryColor}" />

然后您只需将其用作按钮的 Source 属性的值:

   <ImageButton Source="{StaticResource IconBikeSmall}"
                HorizontalOptions="Center"
                VerticalOptions="CenterAndExpand" />

甚至可以像这样直接使用:

  <ImageButton HorizontalOptions="Center"
               VerticalOptions="CenterAndExpand" >
      <ImageButton.Source> 
          <FontImageSource
                 FontFamily="{StaticResource FontAwesome}"
                 Glyph="{x:Static styles:IconFont.Biking}"
                 Size="{StaticResource SizeSmallIcon}"
                 Color="{StaticResource SecondaryColor}" />
      </ImageButton.Source> 
  </ImageButton>                    

【讨论】:

  • 我实际上更喜欢使用它,所以我可以使用按下状态等 - 如果它有效,我将测试并标记为答案。
  • 抱歉,花了这么长时间才回复您 - 这解决了我的问题并且效果很好!谢谢
猜你喜欢
  • 2016-07-10
  • 2020-03-13
  • 2015-07-06
  • 2013-05-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-11
  • 1970-01-01
  • 2015-02-04
相关资源
最近更新 更多