動態設置android:drawableLeft|Right|Top|Bottom
Android中有時需動態設置控件四周的drawble圖片,這個時候就需要調用setCompoundDrawables(left, top, right,bottom),四個參數類型都是drawable
Button繼承TextView,所以可以采用相同的設置方法
方法一.XML方式
Drawable img_on, img_off; Resources res = getResources(); img_off = res.getDrawable(R.drawable.btn_strip_mark_off); //調用setCompoundDrawables時,必須調用Drawable.setBounds()方法,否則圖片不顯示 img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight()); btn.setCompoundDrawables(img_off, null, null, null); //設置左圖標
最後更新:2017-04-03 14:54:38