Calling setCompoundDrawables() doesn't display

2019-01-16 04:09发布

After I call the setCompoundDrawables method, the compound Drawable is not shown..

Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn.setCompoundDrawables(myDrawable, null, null, null);

Any thoughts?

9条回答
Summer. ? 凉城
2楼-- · 2019-01-16 04:55

Use This (I tested). It works good

Drawable image = context.getResources().getDrawable( R.drawable.ic_action );
int h = image.getIntrinsicHeight(); 
int w = image.getIntrinsicWidth();   
image.setBounds( 0, 0, w, h );
button.setCompoundDrawables( image, null, null, null );
查看更多
手持菜刀,她持情操
4楼-- · 2019-01-16 05:04

It is deprecated in API 22.

This code is useful for me:

Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.wen, null);
drawable.setBounds(0, 0, drawable.getMinimumWidth(),
drawable.getMinimumHeight());
tv.setCompoundDrawables(drawable, null, null, null);
查看更多
登录 后发表回答