我重用ImageViews为我的显示,但在某些时候,我没有值把它。

那么如何在Android中清除ImageView呢?

我试过了:

mPhotoView.invalidate();
mPhotoView.setImageBitmap(null);

他们都没有清除视图,它仍然显示以前的图像。


当前回答

我试图通过使用ImageView. setimageressource(0)来删除ImageView的图像,但它对我不起作用。 幸运的是,我在日志中看到了这条信息:

Caused by: java.lang.IllegalStateException: The specified child already has a parent. 
You must call removeView() on the child's parent first.

比如说,layoutmanager是你的布局实例,然后你需要那样做:

RelativeLayout layoutManager = new RelativeLayout(this);
ImageView image = new ImageView(this);

// this line worked for me
layoutManager.removeView(image);

其他回答

这对我很有用。

emoji.setBackground(null);

这会在运行时崩溃。

viewToUse.setImageResource(android.R.color.transparent);  

对于ListView项目图像,您可以在列表适配器中设置ImageView.setVisibility(View.INVISIBLE)或ImageView.setImageBitmap(null),用于“无图像”情况。

我以前和丹尼斯一起干过。谢泼德的解决方案:

viewToUse.setImageResource(0);

它可以工作,但它没有被记录,所以它并不清楚它是否会影响视图中的其他东西(如果你喜欢,你可以检查ImageView代码,我没有)。

我认为最好的解决办法是:

viewToUse.setImageResource(android.R.color.transparent);

我最喜欢这个解决方案,因为在恢复状态方面没有任何棘手的问题,而且它也很清楚它在做什么。

我试图通过使用ImageView. setimageressource(0)来删除ImageView的图像,但它对我不起作用。 幸运的是,我在日志中看到了这条信息:

Caused by: java.lang.IllegalStateException: The specified child already has a parent. 
You must call removeView() on the child's parent first.

比如说,layoutmanager是你的布局实例,然后你需要那样做:

RelativeLayout layoutManager = new RelativeLayout(this);
ImageView image = new ImageView(this);

// this line worked for me
layoutManager.removeView(image);

如果你使用滑翔,你可以这样做。

Glide.with(yourImageView).clear(yourImageView)