1.帶圖片的TextView
1.在xml中設置屬性
<TextView
android:id="@+id/my_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/drawable_shape1"
android:drawableBottom="@mipmap/ic_launcher"
android:drawableLeft="@mipmap/ic_launcher"
android:drawableRight="@mipmap/ic_launcher"
android:drawableTop="@mipmap/ic_launcher"
android:text="22222"
android:textColor="#ffffff"/>
2.如果需要設置圖片大小,只能在代碼中設置
//Returns drawables for the left, top, right, and bottom borders.
Drawable[] compoundDrawables = mMy_Tv.getCompoundDrawables();
for (int i = 0; i < compoundDrawables.length; i++) {
if(i == 3){
//This is where the drawable will draw when its draw() method is called.
compoundDrawables[i].setBounds(0,0,200,200);
}
}
這裏需要注意的是設置完位置之後,需要重新調用setCompoundDrawables()方法,否則圖片會變成這樣:
3.重新設置:
//重新設置 如果沒有圖片用null代替
mMy_Tv.setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], compoundDrawables[2]
,compoundDrawables[3]);