我想获取按钮背景图片的生成 ID。 那个星形图像来自drawable;我想获取它生成的 ID。
我知道 .getId
会获取按钮的特定 ID,但后台资源的 ID 是我想要获取的。没有 button.getBackground().getId()
<Button
android:id="@+id/btnhgfavorite"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/star"
android:focusable="false"/>
最佳答案
似乎您已将可绘制对象本身存储在 Resources 文件夹中。只需从那里获取 id:
int id = R.drawable.star;
就是这样!
如果您想要系统格式化的背景图像,您将无法这样做,因为图像在重新缩放时不会保存,因为这是“在移动中”完成的。
对于 Button 的任何复杂特性和功能,请尝试扩展 Button 并创建自己的类,或者简单地使用不同的类。
关于android - 如何获取按钮的背景id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11311846/