比如Drawable中有一系列连续的图片,img_0.png, img_1.png, img_2.png ...
如果要动态获取这些图片,通过"R.drawable.img_x"的ID方式指定是不行的,因为这个ID无法动态变化,即你是无法通过字符串拼接的方法来实现的。

但可以通过下面方式获取:


String imgname = "img_" + idx;
int imgid = getResources().getIdentifier(imgname, "drawable", "com.ucrobotics.xxx");
EditText.setBackgroundResource(imgid);

根据传递的idx的值不同,就可以动态的获取img_x所对应名称的图片资源了。
其中com.ucrobotics.xxx指的是你的package name,就是Java文件最开始一行的那个。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-23
  • 2021-12-30
  • 2021-04-12
  • 2022-12-23
猜你喜欢
  • 2021-09-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案