如何检索android中的imageViews数组的resource_id?我知道如何让我获得可绘制的资源
int resId = context.getResources().getIdentifier("[imagename]" + indexNumber, "drawable", context.getPackageName());
但现在我想检索一个驻留在layout-folder(activity_main.xml)中的imageView的id
原因是我有10个不同的图像视图,其名称与indexnumer相似。
我的完整代码
ImageView[] imageView = new ImageView[10];
AnimationDrawable[] frameAnimation = new AnimationDrawable[10];
for (int i = 0; i < 10; i++) {
int id = ???
imageView[i] = (ImageView) findViewById(id);
imageView[i].setBackgroundResource(R.drawable.gold_coin_animation);
// Get the background, which has been compiled to an AnimationDrawable object.
frameAnimation[i] = (AnimationDrawable) imageView[i].getBackground();
frameAnimation[i].start();
}
imagevies的名称是
gold_coin_id1
gold_coin_id2
gold_coin_id3
...
gold_coin_id110
所以我想通过与indexnumber连接的前缀(gold_coin)检索视图的resource-id
答案 0 :(得分:1)
这应该有效
int id = getResources().getIdentifier("gold_coin_id"+i, "id", getPackageName());
imageView[i] = (ImageView) findViewById(id);