如何检索android中的imageViews数组的resource_id? 我知道如何让我获得可绘制的资源
int resId = context.getResources().getIdentifier("[imagename]" + indexNumber, "drawable", context.getPackageName());但现在我想检索驻留在layout-folder(activity_main.xml)中的imageView的id
原因是除了indexnumer之外,我有10个不同的图像视图,其名称相似。
我的完整代码
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
How do i retrieve the resource_id for an array of imageViews in android? I know how to get i for a drawable resource
int resId = context.getResources().getIdentifier("[imagename]" + indexNumber, "drawable", context.getPackageName());But now I want to retrieve the id for an imageView which resides in the layout-folder (activity_main.xml)
The reason is that I have 10 different imageviews with similar names except the indexnumer.
My full code
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(); }The names of the imagevies are
gold_coin_id1 gold_coin_id2 gold_coin_id3 ... gold_coin_id110So I want to retrieve the resource-id of the view by the prefix (gold_coin) concatenated with an indexnumber
最满意答案
这应该工作
int id = getResources().getIdentifier("gold_coin_id"+i, "id", getPackageName()); imageView[i] = (ImageView) findViewById(id);This should work
int id = getResources().getIdentifier("gold_coin_id"+i, "id", getPackageName()); imageView[i] = (ImageView) findViewById(id);更多推荐
发布评论