如何以编程方式将颜色值从 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/