我正在尝试学习适用于 Android 的 Java,但不确定如何使用 Google 搜索。我想知道是否有一种方法可以通过编程将数字附加到 R.id.variable 或 R.drawable.variable 等的末尾?
基本上我有几个标记为 image1、image2、image3 等的图像。我想更新 ImageView 可绘制对象以基于另一个变量。因此,如果传递的变量是“1”,它会将 View 更新为“image1”。如果它是“2”,它将更新“image2”等等,但我不知道在调用更新时是否有办法将“1”附加到“R.drawable.image”。
执行此操作的最佳方法是什么?
最佳答案
也许有帮助:
你的图片:image1.png, image2.png, ...., imageN.png
你可以找到他们的id:
getResources().getIdentifier("IMAGE_NAME", "drawable", context.getPackageName())
所以:
int idImage1 = getResources().getIdentifier("image1", "drawable", context.getPackageName())
int idImage2 = getResources().getIdentifier("image2", "drawable", context.getPackageName())
.....
int idImageN = getResources().getIdentifier("imageN", "drawable", context.getPackageName())
关于java - Android Java : R. id.variable1..variable2..variable3..etc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9724472/