1. Background selector (located in res/drawable/, using the method: android:background= "@drawable/xxx")
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<selectorxmlns:android= "Http://schemas.android.com/apk/res/android" >
<itemandroid:drawable= "@drawable/login_blue_pressed" android:state_pressed= "true"/>
<itemandroid:drawable= "@drawable/login_blue_pressed" android:state_focused= "true"/>
<itemandroid:drawable= "@drawable/login_blue_normal"/>
</selector>
2. Text color selector (located in res/color/, using the method: android:textcolor= "@color/xxx")
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<selectorxmlns:android= "Http://schemas.android.com/apk/res/android" >
<itemandroid:color= "#ffffff" android:state_pressed= "true"/>
<itemandroid:color= "#ffffff" android:state_focused= "true"/>
<item android:color= "#000000"/>
</selector>
Note: There is no android:color when Res/drawable/xxx.xml smart prompts, and there is no android:drawable res/color/xxx.xml smart prompts.
3. Background gradient (located in res/color/or res/drawable/)
Res/drawable/selector1.xml:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<itemandroid:state_selected= "true" >
<shape>
<gradientandroid:angle= "270" android:endcolor= "#99BD4C"
Android:startcolor= "#A5D245"/>
<sizeandroid:height= "60DP" android:width= "320DP"/>
<cornersandroid:radius= "8DP"/>
</shape>
</item>
<itemandroid:state_pressed= "true" >
<shape>
<gradientandroid:angle= "270" android:endcolor= "#99BD4C"
Android:startcolor= "#A5D245"/>
<sizeandroid:height= "60DP" android:width= "320DP"/>
<cornersandroid:radius= "8DP"/>
</shape>
</item>
<item>
<shape>
<gradientandroid:angle= "270" android:endcolor= "#A8C3B0"
Android:startcolor= "#C6CFCE"/>
<sizeandroid:height= "60DP" android:width= "320DP"/>
<cornersandroid:radius= "8DP"/>
</shape>
</item>
</selector>
Layout/main.xml:
Copy Code code as follows:
<button
Android:id= "@+id/button1"
Android:layout_width= "100DP"
android:layout_height= "50DP"
android:background= "@drawable/selector1"
android:text= "Button1"/>
Combine shape to achieve a background gradient effect.
or simply use color instead of drawable as the element of the background selector:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<itemandroid:state_selected= "true" >
<shape>
<solidandroid:color= "#ff0000"/>
</shape>
</item>
<itemandroid:state_pressed= "true" >
<shape>
<solidandroid:color= "#ff0000"/>
</shape>
</item>
<item>
<shape>
<solidandroid:color= "#00ffff"/>
</shape>
</item>
</selector>
or drawable and color mix (but pay attention to the four corners of the consistency, because the drawable may arc-shaped Four Corners):
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<selectorxmlns:android= "Http://schemas.android.com/apk/res/android" >
<itemandroid:state_selected= "true" >
<shape>
<solidandroid:color= "#ff0000"/>
</shape>
</item>
<itemandroid:state_pressed= "true" >
<shape>
<solidandroid:color= "#ff0000"/>
</shape>
</item>
<itemandroid:drawable= "@drawable/login_blue_normal" >
</item>
</selector>
Effect Chart:
Reference:
Http://www.shaccp.net/zhishiku/android/220.html
Http://www.cnblogs.com/top5/archive/2012/05/10/2494763.html
http://blog.csdn.net/wswqiang/article/details/6616306