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

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

我试过了:

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

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


当前回答

这对我很有用。

emoji.setBackground(null);

这会在运行时崩溃。

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

其他回答

我也遇到了同样的问题,我把视图的背景色改为布局背景色,你可以这样做:

 edit_countflag.setBackgroundColor(Color.parseColor("#ffffff"));

//然后设置图像

edit_countflag.setImageResource(R.drawable.flag_id);

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

imgView.setImageDrawable(null);

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

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

正如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);

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

这对我来说非常有用!