Android状态选择器

来源:互联网 发布:网络彩票合法吗 编辑:程序博客网 时间:2024/05/16 01:20

转自:http://guofei-715.blog.163.com/blog/static/4416944020109135314385/


button的android:background属性

设置按钮背景图片:onFocus与onClick事件

Item的android:state_focused和 adroid:state_pressed属性

1.main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF25CCDD"
>
<!-- android:background="@drawable/advancedbutton",利用XML来改变按钮背景图片 -->
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/advancedbutton"
/>
<Button
android:id="@+id/button"
android:text="myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
2自定义的advancedbutton.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/onfocusimage2"
/>
<!-- 获得焦点并按下 -->
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/clickimage2"
/>
<!-- 失去焦点时 -->
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/clickimage2"
/>
<!-- 默认时 -->
<item android:drawable="@drawable/defaultimage2"/>
</selector>

CheckBox

<selector xmlns:android="http://schemas.android.com/apk/res/android">


    <!-- Enabled states -->
        
    <item android:state_checked="true" 
        
          android:drawable="@drawable/btn_check_on" />
    <item android:state_checked="false" 
         
          android:drawable="@drawable/btn_check_off" />




</selector>


 

0 0