Android
中有时需动态设置TextView四周的drawble
图片,这个时候就需要调用 setCompoundDrawables(left, top, right, bottom)
方法,四个参数类型都是Drawable
类型。
示例代码如下:
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); //设置左图标