本文翻译自:Calling setCompoundDrawables() doesn't display the Compound Drawable
After I call the setCompoundDrawables
method, the compound Drawable is not shown.. 在调用setCompoundDrawables
方法后,未显示复合Drawable。
Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn.setCompoundDrawables(myDrawable, null, null, null);
Any thoughts? 有什么想法吗?
#1楼
参考:https://stackoom.com/question/ReZq/调用setCompoundDrawables-不会显示Compound-Drawable
#2楼
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 );
#3楼
A little bit simpler again: 再简单一点:
Drawable image = context.getResources().getDrawable(R.drawable.ic_action );
image.setBounds( 0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight() );
button.setCompoundDrawables( image, null, null, null );
#4楼
It is deprecated in API 22. 它在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);
#5楼
Example set to the top: 示例设置为顶部:
view.setCompoundDrawablesWithIntrinsicBounds(
null,
getResources().getDrawable(R.drawable.some_img),
null,
null
);
arguments order: (left, top, right, bottom) 参数顺序:(左,上,右,下)
#6楼
For me setCompoundDrawablesWithIntrinsicBounds(Drawable, Drawable, Drawable, Drawable) did not work. 对我来说, setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable)不起作用。
I had to use setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0) . 我不得不使用setCompoundDrawablesWithIntrinsicBounds(0,0,0,0) 。