我正在尝试用 ContextCompat.GetColor 替换 Resources.GetColor,但最后一个不返回颜色,我不知道我应该使用什么来代替 Resources.GetColor(从 API 23 开始弃用)。任何人都可以帮助我(见下文我想要实现的目标)吗?
Button.SetBackgroundColor(ContextCompat.GetColor(this, Resource.Color.LightRed));
请注意,我使用的是 Xamarin,但如果您有 Java 答案,我可以很容易地对其进行调整。 谢谢!
最佳答案
ContextCompat 只返回颜色的整数表示。您需要通过拆分其 RGB 部分将其转换为 Android 颜色。使用这样的东西
using Android.Graphics;
public static Color GetColorFromInteger(int color)
{
return Color.Rgb(Color.GetRedComponent(color), Color.GetGreenComponent(color), Color.GetBlueComponent(color));
}
在你的方法中
btn.SetBackgroundColor(GetColorFromInteger(ContextCompat.GetColor(this, Resource.Color.LightRed);
关于android - ContextCompat.GetColor 不返回颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33804504/