android - Xamarin (Android) 中资源文件中按钮的 SetBackgroundColor

标签 android button xamarin.android android-resources

我想设置Button 的背景色。我正在使用带有 Xamarin 的 Visual Studio。

在 Android 中我们使用:

Java 代码:

button_vstrong_fluorescence.setBackgroundColor(ContextCompat.getColor(InventoryActivity.this, R.color.linear_filter_background));

但在 Xamarin 中 C# 代码。

button_vstrong_fluorescence.SetBackgroundColor(Android.Graphics.Color.ParseColor("#EBECEC"));

C# 中,上面的代码工作正常。

但每次我都想以静态的方式纠正这件事

Android.Graphics.Color.ParseColor("#EBECEC")

有什么方法可以从我的资源中设置#EBECEC这种颜色。

Color.ParseColor 需要 String 值。

我的尝试:

Android.Graphics.Color.ParseColor(Resource.Colors.linear_filter_background.ToString);

但它给我以下错误。

enter image description here

有没有办法从资源中设置背景颜色?

感谢任何帮助。

最佳答案

不是 Xamarin(或 C#)方面的专家,但像这样的东西应该可以获取您的颜色资源:

button_vstrong_fluorescence
    .SetBackgroundColor(new Android.Graphics.Color(
        ContextCompat.GetColor(this, Resource.Color.linear_filter_background))
    );

关于android - Xamarin (Android) 中资源文件中按钮的 SetBackgroundColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39343019/

相关文章:

c# - Xamarin.Android 在后台线程上使用异步方法会导致屏幕闪烁

c# - 如何在 Xamarin C# Android 中实现 CountDownTimer 类?

android - XML 中的 onClick 属性链接到 Activity 类中的方法

php - Android 上的 httppost 错误

javascript - HTML 按钮不会运行功能

java - 使用 GridLayout 对齐文本字段输入时出现问题

android - SearchView 到你的 ActionBar for xamarin android 中的 Recyclerview?

android - JNI 多个注册调用

android - 设置根据 Intent Extras 值选择的微调项

jQuery:如何制作一个清晰的按钮?