获取图片按钮使用的图片资源名称

Get the name of the image resource used in image button

我是 Android 编程新手,我想知道如何获取当前在图像按钮上设置的图像资源的名称。 例如:b1.setImageResource(R.drawable.img); //其中 b1 是图像按钮,img 是 res/drawable.

中存在的 png 文件的名称

现在我想知道如何获取图像文件的名称,即 "img"。 提前致谢。

您可以使用 setTag() 和 getTag() 这样您可以存储任何您想要的数据。

imageView.setTag("imageNameHere");
String imageName = (String) imageView.getTag();

告诉我! ;)

我猜您只是想将图像添加为按钮的背景。

为此,请将您选择的图片添加到 res/drawable 文件夹中(有多种方法可以做到这一点)。如果愿意,您可以重命名它,但我建议使用带下划线 (_) 的小写字母,例如 "img_1",或者像您所说的 "img"。

然后,在您的 XML 视图中,将 android:background 行添加到您的按钮...

<Button
    android:id="@+id/b1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/img"/>