Android settings drawableLeft (Right / Top / Bottom) in the code

The business needs to be provided in the control code drawableLeft, drawableRight, drawableTop, drawableBottomproperties.

We know that in xml method set as follows:

android:drawableLeft="@drawable/xxxxx"

However, in the code No setDrawableLeft and the like. How to do it?

Do not worry, api provides us with a setCompoundDrawables(left,top,right,bottom);way for developers to set the appropriate boundary picture.

The method of operation is very simple, as shown in the following code:

   // 使用代码设置drawableleft
   Drawable drawable = getResources().getDrawable(
         R.drawable.ic_qaa_top_icon);
   // 这一步必须要做,否则不会显示。
   drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                drawable.getMinimumHeight());
   Button.setCompoundDrawables(null, null, drawable, null);
Published 100 original articles · won praise 45 · views 640 000 +

Guess you like

Origin blog.csdn.net/wangzhongshun/article/details/99595386