1. 程式人生 > >TextView 上下左右設定圖片的兩種方法

TextView 上下左右設定圖片的兩種方法

1,text動態設定圖片方法
 drawable = getResources().getDrawable(R.mipmap.an_icon_2x);
/// 這一步必須要做,否則不會顯示.
 drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight())
//設定text 左邊圖片
text.setCompoundDrawables(drawable, null, null, null); //括號裡面的四個引數代表左上右下四個方向的圖片,可以同時設定多個

//****注意如果不想動態設定圖片,也可以在佈局裡面設定

//2佈局設定上面的圖片
<TextView
    android:id="@+id/home_expert_btn"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:drawableTop="@mipmap/home_2x"
    android:drawablePadding="8dp"
    android:gravity="center"
    android:text="家"
    android:textColor="@color/c505050"
    android:textSize="@dimen/F14" />
android:drawablePadding="8dp"這一句是設定文字和圖片的距離的,可以設定也可以不設定,如果圖片和文字距離不理想可以設定這個引數