问题描述
可能重复:
Android的 - 从@drawable字符串打开资源
首先,遗憾的冠军,但我不知道到底是什么头衔,我可以设置。
First of all sorry for the title but I don't know exactly what title I can set.
好吧,这里是我的问题:
Ok, here's my question:
我会从外部数据库recibe一个字符串,例如:picture0001
I will recibe from a external database a string for example: 'picture0001'.
在该文件夹RES /可绘制我有一个图片这名字是picture0001。
In the folder res/drawable I have a picture which name is picture0001.
我想设置的图片作为ImageView的背景(来源)。
I would like to set that picture as background (source) of a ImageView.
现在的问题是,我该怎么看这个画面,我从外部数据库获得该字符串。
The question is, how can I look for this picture with the string I got from the external database.
感谢你这么多。
推荐答案
是的,您可以通过名称使用看看它<$c$c>Resources.getIdentifier().
Yes, you can look it up by name using Resources.getIdentifier()
.
Context context = imageView.getContext();
int id = context.getResources().getIdentifier("picture0001", "drawable", context.getPackageName());
imageView.setImageResource(id);
这是效率不高,但它的工作原理来查找偶尔的资源。
It's not efficient, but it works to look up occasional resources.
这篇关于查找绘制由字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!