andorid TextView(1)-帶圖片的TextView

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]);

這裏寫圖片描述

發佈了38 篇原創文章 · 獲贊 8 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章