调用setCompoundDrawables()不会显示Compound Drawable

本文翻译自: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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值