lixplor / android-Q-A

🐞 android related questions and answers
0 stars 0 forks source link

setCompoundDrawables不显示 #35

Closed lixplor closed 7 years ago

lixplor commented 7 years ago

setCompoundDrawables不显示

lixplor commented 7 years ago

原因: 没有调用setBounds 解决: 调用setBounds或使用setCompoundDrawablesWithIntrinsicBounds() 代码

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

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

它们两个的区别是:

使用第一个方法类似调用方法如下:
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的图片

以上代码等效于:
myTextview.setCompoundDrawablesWithIntrinsicBounds (Drawable left,Drawable top, Drawable right, Drawable bottom)