Xamarin Forms Button TextColor在Android上的行为不同于iOS [英] Xamarin Forms Button TextColor behaves differently on Android than an iOS

查看:116
本文介绍了Xamarin Forms Button TextColor在Android上的行为不同于iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want a button's text color to be the same on both iOS and Android. So I thought I'd just set the TextColor property in code to the color I wanted. On iOS, the disabled text color is still grey (which is what I want), however, on Android, the disabled text color is the same as the enabled text color. How do I make the disabled text color on Android grey (or whatever its default is for a disabled button)? Thanks

推荐答案

这是Xamarin.Forms中的默认行为.要解决此问题并获得自定义颜色,您可以:

That is the default behavior in Xamarin.Forms. To work around that and achieve a custom color, you can:

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

因为触发器是实现所需目标的最直接,最一致的方法,所以我只会介绍它.您可以查看xamarin 文档了解有关的更多信息自定义渲染器.

Because triggers are the most straightforward and consistent way of achieving what you want, I'll only cover that. You can see the xamarin docs for more information about custom renderers.

使用触发器,可以在未启用时将字体颜色动态更改为灰色.有关触发器的更多信息,请参见文档

Using triggers, you can dynamically change the font color to gray when it is not enabled. See the docs for more information about triggers.

<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>

这篇关于Xamarin Forms Button TextColor在Android上的行为不同于iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆