如何通过变量访问Android drawable

wea*_*ire 2 android

如何通过变量访问Adroid drawable?示例我有drawables:[logo1.png logo2.png ... logoN.png]; 有没有办法让我能做到这一点?

String logopicker="1";
logo.setImageResource(R.drawable.**logo+logopicker**);
Run Code Online (Sandbox Code Playgroud)

嗯,我知道这段代码永远不会运行,但你知道我想做什么.谢谢

Par*_*ani 16

这段代码为我成功运行(我已经在我的应用程序中使用过了),你可以试试这个:

     cnt=1;

     String icon="logo" + cnt;
     int resID = getResources().getIdentifier(icon, "drawable",  getPackageName()); 
     logo.setImageResource(resID); 

     cnt++;  // this require if you want to set images in loop
Run Code Online (Sandbox Code Playgroud)

请享用 !!