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

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

我试过了:

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

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


当前回答

要删除ImageView中的图像,请使用: title_image.setImageResource (1); 这对我很有用

其他回答

我可以通过定义一个可绘制对象(类似于blank_white_shape.xml)来实现这一点:

<shape xmlns:android="http://schemas.android.com/apk/res/android"       
       android:shape="rectangle">
    <solid android:color="@android:color/white"/>
</shape>

当我想清除图像视图时,我只需调用

 imageView.setImage(R.drawable.blank_white_shape);

这对我来说非常有用!

如果这些解决方案都没有清除你已经设置的图像(特别是setImageResource(0)或setImageResources(android.R.color.transparent),检查以确保当前图像没有设置为背景使用setBackgroundDrawable(…)或类似的东西。

你的代码只是将前景中的图像资源设置为你所设置的背景图像前面的一些透明的东西,并且仍然会显示。

我试图通过使用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);

要删除ImageView中的图像,请使用: title_image.setImageResource (1); 这对我很有用

听起来好像你想要的是一个默认图像,当ImageView没有显示不同的图像时设置它。联系人应用程序是这样做的:

if (photoId == 0) {
    viewToUse.setImageResource(R.drawable.ic_contact_list_picture);
} else {
    // ... here is where they set an actual image ...
}