Android在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom。

根据业务的需要,要在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom属性。

我们知道在xml中设置的方法为:android:drawableLeft="@drawable/xxxxx";

但是在代码中并没有相关的setDrawableLeft等方法。怎么办呢?别担心,api为我们提供了一个setCompoundDrawables(left,top,right,bottom);方法,供开发人员设置相应的边界图片。

操作方法十分简单,如下代码所示:

// 使用代码设置drawableleft
        Drawable drawable = getResources().getDrawable(
                R.drawable.ic_qaa_top_icon);
        // / 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(),
                drawable.getMinimumHeight());
        getTv_title().setCompoundDrawables(null, null, drawable, null);

ok,完成

原文地址:https://www.cnblogs.com/tony-yang-flutter/p/3740633.html