Android中resource参数变量使

来源:互联网 发布:seo外包公司北京华网 编辑:程序博客网 时间:2024/05/16 15:20


在Android开发中,经常会用到R.drawable.XXXX来调用资源图片,但这种方式只能直接调用某一个已知名称的资源,如果想动态调用,比如想循环调用图片R.drawable.file1, R.drawable.file2, R.drawable.fileXX等,就不太方便。

如果遇到这种情况,可以用以下代码:

num=1;
String iconname="file" + num;
int resID=getResources().getIdentifier(iconname, "drawable",  getPackageName());
logo.setImageResource(resID);
num++;
 

0 0