如何将颜色保存在array.xml中并返回到Color []数组


Answers:


171

定义您的颜色资源,然后将它们添加到数组中以供访问。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="bright_pink">#FF007F</color>
    <color name="red">#FF0000</color>
    <color name="orange">#FF7F00</color>
    <color name="yellow">#FFFF00</color>
    <color name="chartreuse">#7FFF00</color>
    <color name="green">#00FF00</color>
    <color name="spring_green">#00FF7F</color>
    <color name="cyan">#00FFFF</color>
    <color name="azure">#007FFF</color>
    <color name="blue">#0000FF</color>
    <color name="violet">#7F00FF</color>
    <color name="magenta">#FF00FF</color>

    <array name="rainbow">
        <item>@color/bright_pink</item>
        <item>@color/red</item>
        <item>@color/orange</item>
        <item>@color/yellow</item>
        <item>@color/chartreuse</item>
        <item>@color/green</item>
        <item>@color/spring_green</item>
        <item>@color/cyan</item>
        <item>@color/azure</item>
        <item>@color/blue</item>
        <item>@color/violet</item>
        <item>@color/magenta</item>
    </array>
</resources>

然后像这样访问它们:

int[] rainbow = context.getResources().getIntArray(R.array.rainbow);

for (int i = 0; i < tileColumns; i++) {
    paint.setColor(rainbow[i]);
    // Do something with the paint.
}

3
干净的解决方案。很棒;)
大卫(David

52

如果在array.xml中:

<resources>
    <array name="colors">
        <item>#ffffff</item>
        <item>#000000</item>
    </array>
</resources>

这将为您提供该数组的颜色值:

TypedArray ta = context.getResources().obtainTypedArray(R.array.colors);
int[] colors = new int[ta.length()];
for (int i = 0; i < ta.length(); i++) {
    colors[i] = ta.getColor(i, 0);
}
ta.recycle();

这只是在文档中的TypedArray示例上进行了扩展: http //developer.android.com/guide/topics/resources/more-resources.html#TypedArray

希望能帮助到你!


您甚至可以将颜色资源作为数组中的项进行引用,例如 <array name="colors"> <item>@color/myred</item> <item>@color/myblue</item> </array>
Tim Kist

谢谢。这正是我想要的。
Andrew L.

25

colors.xml

<resources>
    <string-array name="colors">        
        <item>#ff0000</item>
        <item>#00ff00</item>  
        <item>#0000ff</item>
    </string-array>
</resources>

活动类中的代码。

String[] allColors = context.getResources().getStringArray(R.array.colors);

Color.parseColor(allColors[0]) // red
Color.parseColor(allColors[1]) // green
Color.parseColor(allColors[2]) // blue

1
谢谢。我发现这是一个非常不错且干净的解决方案。尤其是因为如果您不需要为列表中的每种颜色指定特定的名称,它几乎没有冗余。
Mathias

12

我无法发表评论,因此必须将其作为新回复。我完全同意使用颜色资源类型的Sky Kelsey wrt设计选择。但是,我发现访问它们的建议方法无效。通过这种方式,我实现了使用XML数组来轻松遍历颜色列表并将颜色应用于各种(自定义绘制)视图的方式。

首先是arrays.xml中的数组:

    <array name="ingr_color_arr">
      <item>@color/ingr_red1</item>
      <item>@color/ingr_orange1</item>
      <item>@color/ingr_yellow1</item>
      <item>@color/ingr_green1</item>
      <item>@color/ingr_blue1</item>
      <item>@color/ingr_violet1</item>
      <item>@color/ingr_red2</item>
      <item>@color/ingr_orange2</item>
      <item>@color/ingr_yellow2</item>
      <item>@color/ingr_green2</item>
      <item>@color/ingr_blue2</item>
      <item>@color/ingr_violet2</item>
   </array>

然后在color.xml中:

<color name="ingr_red1">#FFCC0000</color>
<color name="ingr_orange1">#FFED5F21</color>
<color name="ingr_yellow1">#FFFAE300</color>
<color name="ingr_green1">#FF5B9C0A</color>
<color name="ingr_blue1">#FF0A0D9C</color>
<color name="ingr_violet1">#FF990A9C</color>
<color name="ingr_red2">#FFFFCCCC</color>
<color name="ingr_orange2">#FFFFEACC</color>
<color name="ingr_yellow2">#FFFFFECC</color>
<color name="ingr_green2">#FFC7F5C4</color>
<color name="ingr_blue2">#FFC4DAF4</color>
<color name="ingr_violet2">#FFE1C4F4</color>

然后使用它:

TypedArray ta = res.obtainTypedArray(R.array.ingr_color_arr);
int colorToUse = ta.getResourceId(intGroupNum.intValue() - 1, R.color.recipe_detail_border);
paint.setColor(colorToUse);

这里的关键是使用getResourceId,因为setColor(int)将期望颜色的资源ID。尝试使用getIntArray()or获取值时,出现“找不到资源”错误getColor()

最受欢迎的答案可能有用...我没有尝试过,因为我更喜欢“颜色阵列”设计选择。


1
我也收到“找不到资源”的错误。您的解决方案为我工作。。谢谢
W0rmH0le 2016年

2

在Kotlin中,这要简单得多

 val colors = resources.obtainTypedArray(R.array.colors).use { ta ->
     IntArray(ta.length()) { ta.getColor(it, 0) }
 }

1

如果使用整数数组,则可以直接获取颜色

colors.xml

<color name="easy">#FF0000</color>
<color name="normal">#00FF00</color>
<color name="hard">#0000FF</color>

<integer-array name="colors_difficulty">
    <item>@color/easy</item>
    <item>@color/normal</item>
    <item>@color/hard</item>
</integer-array>

val colorsDif = resources.getIntArray(R.array.colors_dificulty);
view.setBackgroundColor(colorsDif[0])

0

Color.xml:

 <string-array name="listcolors">
        <item>#448AFF</item>
        <item>#FFC107</item>
        <item>#009688</item>
        <item>#ff8000</item>
        <item>#ffbf00</item>
        <item>#0000ff</item>
        <item>#936c6c</item>
        <item>#7733ff</item>
        <item>#7733ff</item>
        <item>#ff8000</item>
        <item>#448AFF</item>
        <item>#0000ff</item>
    </string-array>

activity.java文件

Context context;
 String[] colors = context.getResources().getStringArray(R.array.listcolors);

 String bg_color = colors[i]; //i=1,2,3...

0
<color name="gblue">#4285F4</color>
<color name="ggreen">#34A853</color>
<color name="gyellow">#FBBC05</color>
<color name="gred">#EA4335</color>

<array name="google_colors">
    <item>@color/gblue</item>
    <item>@color/ggreen</item>
    <item>@color/gyellow</item>
    <item>@color/gred</item>
</array>

在java / kotlin中使用此样式,或者在xml中不使用样式

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.