android - 如何获取按钮的背景id

标签 android button

我想获取按钮背景图片的生成 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/

相关文章:

java - 难以理解的日志记录行为

android - Android按下按钮可长时间播放声音

android - 如何从 Cloud Firestore 制作可读的时间戳?

android - 使用 Widget.Sherlock.ActionButton 膨胀 ImageView 时资源不是 Drawable

android - 玻璃 : Null pointer exception when trying to add image to Static Card

button - LinkedIn 按钮间歇性出现

button - 如何使用我自己的自定义按钮并使用 wxpython 进行设计

android - 什么是 HandlerLeak?

python - 在 kivy python 中将按钮的背景更改为不同的形状和样式,如阴影效果等

java - Android 搜索 Activity