【Android】动态设置android:drawableLeft|Right|Top|Bottom

Android中有时需动态设置控件四周的drawble图片,这个时候就需要调用 setCompoundDrawables(left, top, right, bottom),四个参数类型都是drawable
 
Button继承TextView,所以可以采用相同的设置方法
 
方法一.XML方式
 
【Android】动态设置android:drawableLeft|Right|Top|Bottom
<TextView
        android:id="@+id/bookTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/checkmark"
        android:drawableRight="@drawable/checkmark"
        android:drawableTop="@drawable/checkmark"
        android:gravity="center_vertical"
        android:textStyle="bold"
        android:textSize="20dip"
        android:maxLines="1"
        android:ellipsize="end"/>
【Android】动态设置android:drawableLeft|Right|Top|Bottom

 

 

 
方法二.JAVA代码
 
【Android】动态设置android:drawableLeft|Right|Top|Bottom
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); //设置左图标
【Android】动态设置android:drawableLeft|Right|Top|Bottom

 

【Android】动态设置android:drawableLeft|Right|Top|Bottom,布布扣,bubuko.com

【Android】动态设置android:drawableLeft|Right|Top|Bottom

上一篇:【Android】Scrollview 相关问题汇总


下一篇:【转】七、android图片特效处理之光晕效果