How to use Android selector and shape _android

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.