获取Drawable对象:
Resources res = mContext.getResources(); Drawable myImage = res.getDrawable(R.drawable.my_image);
selector是在文件夹drawable中进行定义的xml文件。
它主要定义控件在下pressed,selected,focused及平常状态下的属性。
@H_301_21@ 示例1:
定义背景图片
bg_selector.xml文件
<?xmlversion="1.0"encoding="utf-8"?>
< selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed =" true "
android:drawable =" @drawable/listlayoutsample12xml_press_image "/>
<item
android:state_selected="true"
android:drawable="@drawable/listlayoutsample12xml_focus_image"/>
<item
android:state_focused="true"
android:drawable="@drawable/listlayoutsample12xml_focus_image"/>
<item
android:drawable="@drawable/listlayoutsample12xml_bg_image"/>
</selector>
在布局文件中使用它
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android: background =" @drawable/ bg_selector "
android:paddingRight="13.33dp"
android:layout_height="65.33dp"
android:paddingLeft="31.9dp"
android:gravity="center_vertical">
</LinearLayout>
示例2: