Retrieve a color from values/colors.xml using a variable in name (R.color.name + variable)

余生长醉 提交于 2019-12-06 11:47:10

You can do something like this, assuming your newColors Array is an int Array with the resource ids?

String colorId = "tColor";
Resources resources = getResources();
for (int i = 0; i < numTrails; i++) {
    newColors[i] = resources.getIdentifier(colorId+i, "color", getPackageName());    
}

If it is an array of your colors use getResources().getColor(...) on that result instead:

String colorId = "tColor";
Resources resources = getResources();
for (int i = 0; i < numTrails; i++) {
    int resId = resources.getIdentifier(colorId+i, "color", getPackageName());
    newColors[i] = resources.getColor(resId);
}

can try

refer Typed Array at the last of the page .....

http://developer.android.com/guide/topics/resources/more-resources.html#TypedArray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!