如何setImage为null android的时候,XML已经SRC不为空?为空、时候、null、setImage

2023-09-08 10:02:37 作者:坐在坟里连wifi

我有这样的XML

<ImageView
    android:id="@+id/ImgOn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/icon_on" />

我想通过code有时会删除此图像。我见过很多类似的帖子这个, 但对我来说 imageButton.setBackgroundResource(0);。叶出现在我的XML布局默认图像

I want to remove this image sometimes via code. I've seen many posts like this, but in my case imageButton.setBackgroundResource(0); leaves the default image which appears in my xml layout.

您如何删除它编程?我听说透明色崩溃的一些旧的设备。

How would you remove it programmatically? I heard transparent color crashes some old devices.

更新

setImageResource(0)离开了XML的原SRC以及不为空,因为我希望它是

setImageResource(0) left the xml's original src as well and not null as i want it to be

推荐答案

试试这个..

您设置 setBackgroundResource 这对安卓背景=@可绘制/ icon_on不是机器人:SRC =@可绘制/ icon_on 背景和 SRC 都是不同的使用

Your setting setBackgroundResource that's for android:background="@drawable/icon_on" not for android:src="@drawable/icon_on" background and src both are different use

imageButton.setImageResource(0);

修改

imageButton.setImageDrawable(null);