Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

android - How can I save colors in array.xml and get its back to Color[] array

How can I save color values inside array.xml and retrieve its back to my code as Color [] array?

Thanks beforehand!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Define your color resources, then add them to an array for access.

<?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>

Then access them like this:

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...