android color 一维数组,android - How can I save colors in array.xml and get its back to Color[] array - ...

I can't post a comment, so I must put this in as a new response. I completely agree with Sky Kelsey w.r.t. design choice of using color resource type. However, I found the suggest method to access them did not work. This is the way I implemented the use of an XML array to easily loop through a list of colors and apply the colors to various (Custom painted) views.

First the array in arrays.xml:

@color/ingr_red1

@color/ingr_orange1

@color/ingr_yellow1

@color/ingr_green1

@color/ingr_blue1

@color/ingr_violet1

@color/ingr_red2

@color/ingr_orange2

@color/ingr_yellow2

@color/ingr_green2

@color/ingr_blue2

@color/ingr_violet2

Then in color.xml:

#FFCC0000

#FFED5F21

#FFFAE300

#FF5B9C0A

#FF0A0D9C

#FF990A9C

#FFFFCCCC

#FFFFEACC

#FFFFFECC

#FFC7F5C4

#FFC4DAF4

#FFE1C4F4

Then to use it:

TypedArray ta = res.obtainTypedArray(R.array.ingr_color_arr);

int colorToUse = ta.getResourceId(intGroupNum.intValue() - 1, R.color.recipe_detail_border);

paint.setColor(colorToUse);

The key here is to use getResourceId because setColor(int) is going to expect a resource id for a color. I was getting "Resource not found" errors when I tried getting the value with getIntArray() or getColor().

The most popular answer may work...I didn't try it because I preferred the 'array of colors' design choice better.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值