How to create Drawable from resource

前端 未结 8 1570
臣服心动
臣服心动 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:38

    You must get it via compatible way, others are deprecated:

    Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), R.drawable.my_drawable, null);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题