Set a drawable as background programmatically

前端 未结 3 696
一整个雨季
一整个雨季 2021-02-07 09:32

EDIT: Sorry I realise from your comment my question was not clear enough. I will post a new one. Sorry for this and thanks for your answers

I am populat

3条回答
  •  梦毁少年i
    2021-02-07 09:42

    Now as getDrawable and setBackgroundDrawable both are depricated you should set drawable as Background like this :

    view.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable)); 
    

    and if you are targating minSdk below 16 then make a check like this :

    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.your_drawable));
        } else {
            view.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable));
        }
    

提交回复
热议问题