Set a drawable as background programmatically

前端 未结 3 692
一整个雨季
一整个雨季 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条回答
  • 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));
        }
    
    0 讨论(0)
  • 2021-02-07 09:44

    Here the new Method

    recyclerView.setBackgroundResource(R.drawable.edit_text_button_shape);

    don't use this it's an old method recyclerView.setBackgroundDrawable(this.getResources().getDrawable(edit_text_button_shape));

    0 讨论(0)
  • 2021-02-07 09:48

    Try this

    iv.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.img));
    

    or

    iv.setBackgroundResource(R.drawable.img);
    
    0 讨论(0)
提交回复
热议问题