【问题标题】:How to convert int[] to typedarray Android如何将 int[] 转换为 typedarray Android
【发布时间】:2012-04-27 11:46:15
【问题描述】:
int[] imageIds = { R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4};
TypedArray icons = res.obtainTypedArray(int[]); /* this is not Working. */
【问题讨论】:
标签:
java
android
int
typedarray
【解决方案1】:
您正在传递一个 id 数组,其中只需要一个可绘制数组的 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);