How do you obtain a Drawable object from a resource id in android package?

前端 未结 6 999
别跟我提以往
别跟我提以往 2020-12-12 15:18

I need to get a Drawable object to display on an image button. Is there a way to use the code below (or something like it) to get an object from the android.R.drawable.* pac

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-12 16:05

    best way is

     button.setBackgroundResource(android.R.drawable.ic_delete);
    

    OR this for Drawable left and something like that for right etc.

    int imgResource = R.drawable.left_img;
    button.setCompoundDrawablesWithIntrinsicBounds(imgResource, 0, 0, 0);
    

    and

    getResources().getDrawable() is now deprecated

提交回复
热议问题