在Android中以编程方式删除背景绘制
我想以编程方式删除背景可绘制(@ drawable / bg)。 有没有办法做到这一点?
目前,我的布局中有以下XML:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/widget29" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/bg"> </RelativeLayout> 尝试这个
 RelativeLayout relative = (RelativeLayout) findViewById(R.id.widget29); relative.setBackgroundResource(0); 
检查RelativeLayout文档中的setBackground函数
  setBackgroundResource(0)是最好的select。 从文档 : 
将背景设置为给定的资源。 资源应该引用一个可绘制的对象或0来删除背景 。
它在任何地方都可以工作,因为它是从API 1起的。
  setBackground稍后在API 16中添加,因此,如果minSdkVersion低于16,则不起作用。 
 这帮助我删除背景颜色,希望它可以帮助别人。 setBackgroundColor(Color.TRANSPARENT) 
试试这个代码:
 imgView.setImageResource(android.R.color.transparent); 
也是这个作品:
 imgView.setImageResource(0); 
但要小心这一个不工作:
 imgView.setImageResource(null); 
首先,你必须写
  android:visibility="invisible" <!--or set VISIBLE--> 
然后用这个来显示它
  myimage.setVisibility(SHOW);//HIDE 
我尝试在Android 4 +这个:
  view.setBackgroundDrawable(0); 
此方法的最佳性能:
 imageview.setBackgroundResource(R.drawable.location_light_green); 
用这个。
 使用setBackgroundColor(Color.TRANSPARENT)将背景设置为透明,或使用setBackgroundColor(0) 。 这里Color.TRANSPARENT是颜色类的默认属性。 它会正常工作。 
我有一个案例情景,我尝试了上面的所有答案,但是总是在旧的上面创build新的图像。 为我工作的解决scheme是:
 imageView.setImageResource(R.drawable.image); 
除了优秀的答案,如果你想通过XML实现这个,那么你可以添加:
 android:background="@android:color/transparent 
以你的看法。
这为我工作:
 yourview.setBackground(null);