【发布时间】:2016-11-30 05:02:22
【问题描述】:
如何在 android 中检索 imageViews 数组的 resource_id?我知道如何为可绘制资源获取 i
int resId = context.getResources().getIdentifier("[imagename]" + indexNumber, "drawable", context.getPackageName());
但现在我想检索位于布局文件夹 (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();
}
图像的名称是
gold_coin_id1
gold_coin_id2
gold_coin_id3
...
gold_coin_id110
所以我想通过前缀 (gold_coin) 与 indexnumber 连接来检索视图的资源 ID
【问题讨论】:
-
你可以像 imageViewArray[i].setId(i); 一样将 id 设置为 imageview
-
ImageView[] imageViewArray = new ImageView[10]; for (int i = 0; i
-
你可以创建 int[] resArray = new int[]{ R.drawable.gold_coin_id1, R.drawable.gold_coin_id2, R.drawable.gold_coin_id3, R.drawable.gold_coin_id4} 并使用这个 resArray设置背景
标签: android android-imageview android-xml