button - Xamarin Forms Button TextColor 在 Android 上的表现与在 iOS 上不同

标签 button xamarin.forms textcolor

我希望按钮的文本颜色在 iOS 和 Android 上都相同。所以我想我只需将代码中的 TextColor 属性设置为我想要的颜色。在 iOS 上,禁用的文本颜色仍然是灰色(这是我想要的),但是,在 Android 上,禁用的文本颜色与启用的文本颜色相同。如何使 Android 上的禁用文本颜色变为灰色(或禁用按钮的默认颜色)?
谢谢

最佳答案

这是 Xamarin.Forms 中的默认行为。要解决该问题并实现自定义颜色,您可以:

  • 从启用到禁用时使用触发器更改颜色,反之亦然
  • 启用和禁用按钮时手动设置颜色
  • 使用自定义渲染器

  • 因为触发器是实现您想要的目标的最直接、最一致的方式,所以我只会介绍这一点。您可以看到 xamarin docs有关自定义渲染器的更多信息。

    使用触发器,您可以在未启用时将字体颜色动态更改为灰色。见docs有关触发器的更多信息。
    <Button Text="I am a Button">
    <Button.Triggers>
        <Trigger TargetType="Button"
             Property="IsEnabled" Value="true">
            <Setter Property="TextColor" Value="Red" />
            <!-- red is the desired color when the button is enabled.-->
        </Trigger>
        <Trigger TargetType="Button"
             Property="IsEnabled" Value="false">
            <Setter Property="TextColor" Value="Gray" />
            <!-- gray is the desired color when the button is disabled.-->
        </Trigger>
    </Button.Triggers>
    </Button>
    

    关于button - Xamarin Forms Button TextColor 在 Android 上的表现与在 iOS 上不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32402234/

    相关文章:

    ios - ITMS-90809:不推荐使用的API使用情况(UIWebView)

    r - 在 Plotly for R 中自定义文本(文本大小、文本颜色、文本角度)

    android - TextView textColor 中的数据绑定(bind)选择器

    css - 图像顶部的按钮(但它不会停留)

    css - Bootstrap 3,覆盖btn.active,点击事件在anchor元素后1px消失

    java - 如何用按钮停止循环?

    自上次使用相同代码构建以来,Xamarin iOS 应用程序大小增加了 60%

    jQuery-ui:启用禁用按钮不会恢复事件

    c# - 将图像复制到另一个文件夹

    android - 如何设置android :textColor using drawable in Android?