Android 颜色资源

Bom*_*Bus 2 resources android colors

<resources>

<color name="red">#e51c23</color>
<color name="pink">#e91e63</color>
<color name="purple">#9c27b0</color>
<color name="deep_purple">#673ab7</color>

<string-array name="colors_hex_code">
    <item>@color/red</item>
    <item>@color/pink</item>
    <item>@color/purple</item>
    <item>@color/deep_purple</item>
</string-array>

</resources>
Run Code Online (Sandbox Code Playgroud)

你好,我像代码一样声明了colors.xml,当我访问这个值时,就像java一样

String[] s = getResources().getStringArray(R.array.colors_hex_code);

Toast.makeText(getActivity(), "First Color: " + s[0], Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)

,为什么 s[index] 总是返回 null?我想从“colors_hex_code”字符串数组中获取十六进制颜色代码。可以这样访问吗?请帮忙。感谢你。

小智 5

将字符串数组更改为整数数组:

<resources>

<color name="red">#e51c23</color>
<color name="pink">#e91e63</color>
<color name="purple">#9c27b0</color>
<color name="deep_purple">#673ab7</color>

<integer-array name="colors_hex_code">
    <item>@color/red</item>
    <item>@color/pink</item>
    <item>@color/purple</item>
    <item>@color/deep_purple</item>
</integer-array>

</resources>
Run Code Online (Sandbox Code Playgroud)

和Java代码:

int[] s = getResources().getIntArray(R.array.colors_hex_code);

Toast.makeText(getActivity(), "First Color: " + String.format("#%06X", (0xFFFFFF & s[0])), Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)