Android imagebutton 以编程方式更改?

标签 android xml drawable imagebutton

你好,我有一个图像按钮 linearButton,它在 XML 中设置了背景可绘制对象。我想有条件地替换代码中的背景,但它从来没有发生过!

Drawable replacer = getResources().getDrawable(R.drawable.replacementGraphic);
linearButton.setBackgroundDrawable(replacer);

这似乎是无效的,是否有一个图像按钮的“重新加载”函数,我必须在它们视觉上发生变化之前调用它?

最佳答案

invalidate() 方法将强制重绘任何 View :

Drawable replacer = getResources().getDrawable(R.drawable.replacementGraphic);
linearButton.setBackgroundDrawable(replacer);
linearButton.invalidate();

参见 here供引用。

关于Android imagebutton 以编程方式更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6751875/

相关文章:

c# - 构建 JSON 配置部分

android - Log4jshell 会使我的 android 应用程序易受攻击吗?如果是这样,我该如何保护我的应用程序

android - 无法将文本插入到我的 SQLite 数据库中

android - 处理强行关闭?

java - 检测到 Android Studio 项目

android - 使用内置的 Android 图像查看器打开存储在 res/drawable 中的图片

android - android手机usb绑定(bind)后如何获取系统ip地址?

xml - 从哈希表输入生成xml输出

android - 以编程方式添加具有纯色和描边的渐变

java - 从 SD 卡加载绘图