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

后端 未结 8 1225
借酒劲吻你
借酒劲吻你 2020-12-04 21:01

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

Thanks beforehand!

相关标签:
8条回答
  • 2020-12-04 21:38

    colors.xml

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

    Code in activity class.

    String[] allColors = context.getResources().getStringArray(R.array.colors);
    
    Color.parseColor(allColors[0]) // red
    Color.parseColor(allColors[1]) // green
    Color.parseColor(allColors[2]) // blue
    
    0 讨论(0)
  • 2020-12-04 21:38
    <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>
    

    Use this in java/kotlin or style do not use in xml

    0 讨论(0)
提交回复
热议问题