How can i access an Android drawable by a variable

前端 未结 1 1347
梦毁少年i
梦毁少年i 2021-01-03 14:11

How can i access an Adroid drawable by a variable? Example I have drawables :[ logo1.png logo2.png ... logoN.png]; Is there a way so i can do that?

String l         


        
1条回答
  •  有刺的猬
    2021-01-03 14:27

    This code is running for me successful (I have already used in my one of the application), You can Try this:

         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
    

    Enjoy !!

    0 讨论(0)
提交回复
热议问题