How to create Drawable from resource

前端 未结 8 1602
臣服心动
臣服心动 2020-12-04 07:48

I have an image res/drawable/test.png (R.drawable.test).
I want to pass this image to a function which accepts Drawable, e.g. mButton.set

8条回答
  •  青春惊慌失措
    2020-12-04 08:48

    This code is deprecated:

    Drawable drawable = getResources().getDrawable( R.drawable.icon );
    

    Use this instead:

    Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),R.drawable.icon);
    

提交回复
热议问题