Button或TextView设置drawableLeft图片(左图标)

1、XML方式

  1. <Button android:drawableLeft="@drawable/btn_strip_mark_on"></Button>
复制代码

2、JAVA代码

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); //设置左图标

 setBounds()的作用

setBounds(x,y,width,height); x:组件在容器X轴上的起点 y:组件在容器Y轴上的起点 width:组件的长度 height:组件的

猜你喜欢

转载自biandroid.iteye.com/blog/1633277