在TextView中设置DrawableLeft不显示的问题

1.在XML中使用 android:drawableLeft="@drawable/icon"

2.代码中动态变化 Drawable drawable= getResources().getDrawable(R.drawable.drawable);

/// 这一步必须要做,否则不会显示. drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); myTextview.setCompoundDrawables(drawable,null,null,null);

注意必须调用setBounds,才能腾出空间来显示drawableLeft的图片

以上代码等效于:

setCompoundDrawablesWithIntrinsicBounds (Drawable left,Drawable top, Drawable right, Drawable bottom)

Drawable的setBounds方法有四个参数,setBounds(int left, int top, int right, int bottom),这个四参数指的是drawable将在被绘制在canvas的哪个矩形区域内。
原文地址:https://www.cnblogs.com/androidsj/p/5073679.html