Android:Textview 通过代码设置 Drawable

本文介绍了解决在Android TextView中添加Drawable图标不显示的问题。通过详细解释如何正确设置Drawable的边界,确保Drawable能在TextView中正常显示。

解决方案

public void  setCompoundDrawables  (Drawable left, Drawable top, Drawable right, Drawable bottom);  

类似与在 XML 中

android:drawableLeft="@drawable/icon"  

 

使用 TextView 直接调用 setCompoundDrawables() 就可以实现添加 drawable,但是添加完成以后无法显示,查看源码,发现有这么一句话

The Drawables must already have had {@link Drawable#setBounds} called.

1 Drawable drawable = context.getResources().getDrawable(R.drawable
2                         .todo_date_alert_2x);
3 drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
4 holder.todoDataALLDate.setCompoundDrawables(drawable, null, null, null);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值