Remove background drawable programmatically in Android

前端 未结 11 1491
忘掉有多难
忘掉有多难 2020-12-07 17:17

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:

相关标签:
11条回答
  • 2020-12-07 17:35

    This work for me:

    yourview.setBackground(null);
    
    0 讨论(0)
  • 2020-12-07 17:38

    Try this

    RelativeLayout relative = (RelativeLayout) findViewById(R.id.widget29);
    relative.setBackgroundResource(0);
    

    Check the setBackground functions in the RelativeLayout documentation

    0 讨论(0)
  • 2020-12-07 17:45

    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.

    0 讨论(0)
  • 2020-12-07 17:47

    I try this code in android 4+:

    view.setBackgroundDrawable(0);
    
    0 讨论(0)
  • 2020-12-07 17:47

    In addition to the excellent answers, if you want to achieve this via xml then you can add:

    android:background="@android:color/transparent

    to your view.

    0 讨论(0)
  • 2020-12-07 17:48

    Best performance on this method :

    imageview.setBackgroundResource(R.drawable.location_light_green);
    

    Use this.

    0 讨论(0)
提交回复
热议问题