Android开发技巧不同状态的Button

来源:互联网 发布:dsp算法优化 编辑:程序博客网 时间:2024/04/29 05:49

使用XML实现按钮改变焦点设置背景图,获得焦点时,获得焦点并按下,失去焦点时,默认时...

新建res/drawable/button.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- 获得焦点时 --><itemandroid:state_focused="true"android:state_pressed="false"android:drawable="@drawable/focusimage"/><!-- 获得焦点并按下 --><itemandroid:state_focused="true"android:state_pressed="true"android:drawable="@drawable/clickimage"/><!-- 失去焦点时 --><itemandroid: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>



原创粉丝点击