问题描述
我有一堆2.JPG ......和56.jpg命名的图像,1.JPG的。我也有在其上从previous列表活性选择的项目中的可变依赖性的_id值。我有以下的code来显示图像。
I have a bunch of images named, 1.jpg, 2.jpg… and 56.jpg. I also have an _id value in a variable dependent on which item was selected from the previous list activity. I have the following code to display an image.
ImageView image = (ImageView) findViewById(R.id.headerimage);
image.setImageResource(R.drawable.image1);
我想要做的是动态显示与_id值对应的图像,例如..这样的事情?
What I would like to do is dynamically display the image that corresponds with the _id value, e.g.. something like this?
ImageView image = (ImageView) findViewById(R.id.headerimage);
image.setImageResource(R.drawable.myID + ".jpg");
我该怎么办呢?
干杯,
麦克。
推荐答案
是的,你可以做到这一点。
Yes, you can do this.
只需使用 Resouces.getIdentifier()。请注意,这个实现是慢,使用的ID和经常检查RESOURCEID不为0 (这将意味着你要的资源不会退出。
Just use Resouces.getIdentifier(). Be aware that this implementation is slower that using the id and always check if the resourceId is not 0 (that would mean the resource you asked for doesn't exit.
例如:
View view = //...
int number = //...
int resId = getIdentifier("files_"+number, "drawable", "com.my.project.package");
view.setBackgroundResource(resId);
这篇关于Android的动态显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!