TextView的setCompoundDrawables不显示问题解决

TextView的setCompoundDrawables不显示问题解决

 
 
 

  TextView可以用过调用setCompoundDrawables设置一张图片出现在上下左右四个地方。代码如下:

  Drawable drawable = getResources().getDrawable(R.drawable.img);
        drawable.setBounds(0, 0, 32, 32);
        textView.setCompoundDrawables(drawable, null, null, null);

  注意到这个Drawables必须已经调用过了setBounds。如果不设置则无法显示。

项目中的代码:

public static void setTextDrawableRight(Context context, TextView tv, int id) {
Drawable drawable = context.getResources().getDrawable(id);
if (drawable != null) {
drawable.setBounds(0, 0, Units.dip2px(12f), Units.dip2px(6f));
}
tv.setCompoundDrawables(null, null, drawable, null);
}
原文地址:https://www.cnblogs.com/fightzhao/p/4755885.html