使用XML实现按钮改变焦点设置背景图,获得焦点时,获得焦点并按下,失去焦点时,默认时...
新建res/drawable/button.xml
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <!-- 获得焦点时 -->
- <item
- android:state_focused="true"
- //抬起按下时的状态
- android:state_pressed="false"
- android:drawable="@drawable/focusimage"
- />
- <!-- 获得焦点并按下 -->
- <item
- android:state_focused="true"
- android:state_pressed="true"
- android:drawable="@drawable/clickimage"
- />
- <!-- 失去焦点时 -->
- <item
- android:state_focused="false"
- android:state_pressed="true"
- android:drawable="@drawable/clickimage"
- />
- <!-- 默认时 -->
- <item android:drawable="@drawable/normal"/>
- </selector>
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <Button
- android:layout_marginTop="20dp"
- android:id="@+id/button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="不同状态的Button"
- android:background="@drawable/button"/>
- </LinearLayout>