我的问题是如何在 android 的 setImageResource() 中更改 drawable 文件夹中的图像 id。 我的可绘制文件夹包含 icon0.png 到 icon9.png,我想使用此动态更改 ImageView 中的这些图像
ImageView iV3;
iV3 = (ImageView) findViewById(R.id.imageView3);
iV3.setImageResource(R.drawable.icon + speed_Arr[2]);
speed_Arr[2] 包含 0 - 9 之间的任何值。 但这并没有改变图像。 请帮帮我。 问候。
最佳答案
public static int getIdentifier(Context context, String name)
{
return context.getResources().getIdentifier(name.substring(0, name.lastIndexOf(".")), "drawable", context.getPackageName());
}
以上代码将从名称字符串中返回资源 ID。
关于android - 如何在 setImageResource() Android 中动态更改 drawable 的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9189728/