获取资源ID Imageview(ImageViews数组)(Get resource id Imageview (array of ImageViews))

编程入门 行业动态 更新时间:2024-10-25 01:22:21
获取资源ID Imageview(ImageViews数组)(Get resource id Imageview (array of ImageViews))

如何检索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_id110

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

更多推荐

本文发布于:2023-08-01 02:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1352811.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   资源   Imageview   ImageViews   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!