c# - Xamarin Android : getting color value from colors. xml 以编程方式

标签 c# android xml xamarin

如何以编程方式将颜色值从 colors.xml 文件获取到 C# 代码中?

这是我的 colors.xml:

<?xml version="1.0" encoding="utf-8" ?>
<resources>

  <item name="row_a" type="color">#FFCCFFCC</item>
  <item name="row_b" type="color">#FFFFFFCC</item>
  <item name="all_text" type="color">#FF000000</item>
  <item name="row_red" type="color">#FFFF4444</item>
  <item name="row_orange" type="color">#FFE69900</item>
  <item name="row_green" type="color">#FF739900</item>
  <item name="wheat" type="color">#FFF5DEB3</item>

  <integer-array name="androidcolors">
    <item>@color/row_a</item>
    <item>@color/row_b</item>
    <item>@color/all_text</item>
    <item>@color/row_red</item>
    <item>@color/row_orange</item>
    <item>@color/row_green</item>
    <item>@color/wheat</item>
  </integer-array>

</resources>

我试过:

Color t = (Color)Resource.Colors.wheat;

但我当然不能通过这种方式将 int 值转换为 Color。

编辑:

按照建议我试过了

Color t = Resources.GetColor(Resource.Color.row_a);

但它给了我一个错误:

Error   CS0120  An object reference is required for the non-static field, 
method, or property 'Resources.GetColor(int)'

最佳答案

试试这段代码:

  Color t = new Android.Graphics.Color (ContextCompat.GetColor (this, Resource.Color.row_a)); 

关于c# - Xamarin Android : getting color value from colors. xml 以编程方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34723855/

相关文章:

android - 弃用的 ActionBarDrawerToggle

php - 从 PHP 中的 XML 链接获取下一个标题

java - XML 解析器示例

c# - 使用 LINQ 检查枚举器值(如果存在)

android - Fragments ConstraintLayout 是在 activity 的 FrameLayout 中截取的

javascript - 脚本在 Web 浏览器控件中不起作用

android - 如何删除工具栏中的设置?

c# - Xml 文档未打包在类库中

c# - 枚举创建断点

C#:异步运行此代码的不同方法?