gpt4 book ai didi

android - android 可以像整数数组一样存储可绘制对象 ID 吗?

转载 作者:太空狗 更新时间:2023-10-29 15:41:13 25 4
gpt4 key购买 nike

我想要一个整数值的 drawable id 数组,我可以像 integer-array 一样存储在 res/values/XXX.xml 中通过使用 integer-array 标签。下面是在 strings.xml

中声明的整数数组
<integer-array name="icons">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</integer-array>

但我想将像 @drawable/someImage 这样的可绘制图像 ID 存储为 xml 中的整数数组。

是否有任何替代方法可以将可绘制整数 ID 作为整数数组存储在 xml 中。

最佳答案

我认为 TypedArray 是您要查找的内容。我有使用它的 sample 。如果您有兴趣,请查看以下代码:

首先res/values/arrays.xml中的integer-array:

<integer-array name="frag_home_ids">
<item>@drawable/frag_home_credit_return_money</item>
<item>@drawable/frag_home_transfer</item>
<item>@drawable/frag_home_balance</item>
<item>@drawable/frag_home_charge</item>
<item>@drawable/frag_home_finance_cdd</item>
<item>@drawable/frag_home_finance_ybjr</item>
<item>@drawable/frag_home_more</item>
</integer-array>

其次,以编程方式获取资源整数值:

TypedArray tArray = getResources().obtainTypedArray(
R.array.frag_home_ids);
int count = tArray.length();
int[] ids = new int[count];
for (int i = 0; i < ids.length; i++) {
ids[i] = tArray.getResourceId(i, 0);
}
//Recycles the TypedArray, to be re-used by a later caller.
//After calling this function you must not ever touch the typed array again.
tArray.recycle();

第三种,像这样调用整数值:

holder.iv.setImageResource(ids[position]);

当然可以得到string,color,integer,layout,的整数值>菜单...以这种方式。

希望这些代码对您有所启发。

关于android - android 可以像整数数组一样存储可绘制对象 ID 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29819204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com