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"這一句是設置文字和圖片的距離的,可以設置也可以不設置,如果圖片和文字距離不理想可以設置這個參數