如何将int []转换为typedarray Android

时间:2012-04-16 05:40:16

标签: java android int typedarray

int[] imageIds = { R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4};

TypedArray icons = res.obtainTypedArray(int[]); /* this is not Working. */

1 个答案:

答案 0 :(得分:2)

你传递的是一个id的数组,其中只需要一个drawable数组的id。

请参阅此示例..

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="icons">
        <item>@drawable/home</item>
        <item>@drawable/settings</item>
        <item>@drawable/logout</item>
    </array>
    <array name="colors">
        <item>#FFFF0000</item>
        <item>#FF00FF00</item>
        <item>#FF0000FF</item>
    </array>
</resources>
This application code retrieves each array and then obtains the first entry in each array:
Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);

TypedArray colors = res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0,0);