难道Android商店绘制ID,如整数数组? [英] Could android store drawable ids like an integer-array?

查看:282
本文介绍了难道Android商店绘制ID,如整数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要一个绘制整数值的ID数组,我可以储存像整数数组 RES /价值/ XXX.xml 使用整数数组标记。下面是的strings.xml

I want a drawable id array of integer values which I can store like an integer-array in res/values/XXX.xml by using integer-array tag. Below is integer-array declared in strings.xml

<integer-array name="icons">
     <item>1</item>
     <item>2</item>
     <item>3</item>
     <item>4</item>
</integer-array>

但我想绘制的图像ID,如存储@绘制/ someImage 作为XML的整数数组。

But I want to store drawable image ids like @drawable/someImage as an integer array in xml.

有其它方法来存储绘制整数ID作为XML的整数数组。

OR Is there any alternatives to store drawable integer ids as an integer array in xml.

推荐答案

我觉得 TypedArray 是你在找什么。我有样使用它。如果你有兴趣,就来看看$ C $以下CS:

I think TypedArray is what you are looking for. I have samples using it. If you are interested, take a look at codes below:

第一整数数组 RES /价值/ arrays.xml

<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>

,获得资源整数值编程方式:

Second, get resource integer values programmatically:

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);
}

第三,调用整数值是这样的:

Third, call the integer values like this:

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

当然,你可以得到字符串颜色整的整数值布局菜单 ...这个样子。

Of course, you can get integer values of string, color, integer, layout, menu...in this way.

我希望这些codeS会对你有所启发。

I hope these codes will inspire you.

这篇关于难道Android商店绘制ID,如整数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆