Remove background drawable programmatically in Android

前端 未结 11 1555
忘掉有多难
忘掉有多难 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: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.

提交回复
热议问题