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

后端 未结 8 1273
借酒劲吻你
借酒劲吻你 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:17

    If this is in array.xml:

    
        
            #ffffff
            #000000
        
    
    

    This will give you the color values for that array:

    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();
    

    This just expands on the TypedArray example in the docs: http://developer.android.com/guide/topics/resources/more-resources.html#TypedArray

    Hope it helps!

提交回复
热议问题