根據業務的需要,要在代碼中設置控件的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,完成