1. 程式人生 > >Android TextView 程式碼中設定DrawableLeft 圖片

Android TextView 程式碼中設定DrawableLeft 圖片

xml中的textView的drawableLeft是這樣設定的

<TextView  
	android:id="@ id/demoTextView"  
	android:layout_width="wrap_content"  
	android:layout_height="wrap_content"  
	android:drawableLeft="@drawable/drawable_left_icon"  
<span style="white-space:pre">	</span>android:text="你好"
	android:gravity="center_vertical"  
	android:textSize="10dp" />  
使用程式碼為textview設定drawableLeft
Drawable drawable = getResources().getDrawable(R.drawable.icon_sign_flashlight_closed);// 找到資源圖片
// 這一步必須要做,否則不會顯示。
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());// 設定圖片寬高
tv_flashlight_top.setCompoundDrawables(drawable, null, null, null);// 設定到控制元件中