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);
}

相关文章:

  • 2021-05-28
  • 2022-12-23
  • 2021-09-27
  • 2022-12-23
  • 2021-07-13
  • 2022-12-23
  • 2021-12-29
  • 2021-06-02
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-16
  • 2022-12-23
  • 2022-12-23
  • 2021-07-17
  • 2021-07-19
相关资源
相似解决方案