Remove background drawable programmatically in And

2019-01-21 08:44发布

I want to remove the background drawable (@drawable/bg) programmatically. Is there a way to do that?

Currently, I have the following XML in my layout:

<?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>

11条回答
Root(大扎)
2楼-- · 2019-01-21 09:10

I have a case scenario and I tried all the answers from above, but always new image was created on top of the old one. The solution that worked for me is:

imageView.setImageResource(R.drawable.image);
查看更多
时光不老,我们不散
3楼-- · 2019-01-21 09:10

This work for me:

yourview.setBackground(null);
查看更多
萌系小妹纸
4楼-- · 2019-01-21 09:12

setBackgroundResource(0) is the best option. From the documentation:

Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.

It works everywhere, because it's since API 1.

setBackground was added much later, in API 16, so it will not work if your minSdkVersion is lower than 16.

查看更多
女痞
5楼-- · 2019-01-21 09:14

First, you have to write

 android:visibility="invisible" <!--or set VISIBLE-->

then use this to show it

 myimage.setVisibility(SHOW);//HIDE                                       
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-21 09:15

Try this code:

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

also this one works:

imgView.setImageResource(0); 

but be careful this one doesn't work:

imgView.setImageResource(null); 
查看更多
登录 后发表回答