我正在重复使用 ImageView
来显示数据,但有时候我没有值来显示。
那么,如何清除 Android 中的 ImageView
呢?
我尝试过:
mPhotoView.invalidate();
mPhotoView.setImageBitmap(null);
它们中没有一个清除了视图,它仍然显示先前的图像。
我正在重复使用 ImageView
来显示数据,但有时候我没有值来显示。
那么,如何清除 Android 中的 ImageView
呢?
我尝试过:
mPhotoView.invalidate();
mPhotoView.setImageBitmap(null);
它们中没有一个清除了视图,它仍然显示先前的图像。
对于ListView项的图像,您可以在列表适配器中设置ImageView.setVisibility(View.INVISIBLE)
或ImageView.setImageBitmap(null)
来处理“无图像”情况。
AppCompatImageView
而不是ImageView
,它支持在所有设备上使用setImageResource(0)
,因此您可以像使用setImageDrawable(null)
和setImageBitmap(null)
一样使用它。setImageResource(0)
应该是安全的。您可以查看API 22与API 21的Android代码,这里。我曾经遇到过同样的问题,我将视图的背景颜色更改为布局的背景颜色,你也可以这样做:
edit_countflag.setBackgroundColor(Color.parseColor("#ffffff"));
//然后设置图片
edit_countflag.setImageResource(R.drawable.flag_id);
要移除 ImageView 中的图像,请使用:
title_image.setImageResource(-1);
这对我有效。
我尝试使用ImageView.setImageRessource(0)来删除ImageView的图像,但对我没起作用。
幸运地是,我在日志中得到了这个消息:
<code>Caused by: java.lang.IllegalStateException: The specified child already has a parent.
You must call removeView() on the child's parent first.
</code>
所以假设 “layoutmanager” 是你的布局实例,那么你需要这样做:
RelativeLayout layoutManager = new RelativeLayout(this);
ImageView image = new ImageView(this);
// this line worked for me
layoutManager.removeView(image);
我可以指出你们都试图将一个int设置为drawable。
难道你们不应该这样做吗?
imageview.setImageDrawable(this.getResources().getDrawable(R.drawable.icon_image));
imageview.setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.icon_profile_image));
editText.setText("");
imageView.setImageResource(0);