当xml的src不为null时,如何在android中将setImage设置为null?

我有这个xml

 

我想有时通过代码删除此图像。 我见过很多像这样的post,但在我的例子中是imageButton.setBackgroundResource(0); 保留我的xml布局中显示的默认图像。

你会如何以编程方式删除它? 我听说透明的颜色会让一些旧设备崩溃。

更新

setImageResource(0)也离开了xml的原始src而不是我想要的null

你可以使用imageButton.setImageResource(0)

试试这个..

你的设置setBackgroundResource是为android:background="@drawable/icon_on"不是为了android:src="@drawable/icon_on" 背景src都是不同的用法

 imageButton.setImageResource(0); 

编辑

 imageButton.setImageDrawable(null); 

背景(backgroundresource)和前景(src)图像是两个不同的东西,因此一个不影响另一个。

使用以下方法删除前景图像

 imageButton.setImageDrawable(null); 

如果你想null imageview那么你必须调用imageButton.setImageResource(0); BackgroundResource只为null图像背景