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

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

我试过了:

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

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


当前回答

我可以通过定义一个可绘制对象(类似于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);

这对我来说非常有用!

其他回答

editText.setText (" ");

imageView.setImageResource(0);

正如kwasi写的和golu编辑的,你可以使用透明,而不是白色:

文件可拉的/ transparent.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@android:color/transparent"/>
</shape>

在活动、视图等内部:

view.setImageResource(R.drawable.transparent);

我有一个类似的问题,我需要从屏幕上完全删除ImageViews。这里的一些答案引导我走向正确的方向,但最终调用setImageDrawable()对我有用:

imgView.setImageDrawable(null);

(正如评论中提到的,这种用法在官方文档中有记录:imageview# setImageDrawable。)

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

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

我可以通过定义一个可绘制对象(类似于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);

这对我来说非常有用!