How to Use Android Selector and Shape

Source: Internet
Author: User

1. background selector (IN res/drawable/, usage: android: background = "@ drawable/XXX ")

Copy codeThe Code is 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 (IN res/color/, usage: android: textColor = "@ color/XXX ")Copy codeThe Code is 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" type = "codeph" text = "/codeph"/>
</Selector>

Note: android: color is not displayed when res/drawable/XXX. xml is displayed. Similarly, android: drawable is not displayed when res/color/XXX. xml is displayed.
3. Background gradient (IN res/color/or res/drawable /)
Res/drawable/selector1.xml:Copy codeThe Code is 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 codeThe Code is as follows: <Button
Android: id = "@ + id/button1"
Android: layout_width = "100dp"
Android: layout_height = "50dp"
Android: background = "@ drawable/selector1"
Android: text = "button1"/>

Use Shape to achieve the gradient effect of the background.
Or use color instead of drawable as the background selector element:Copy codeThe Code is 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 = "#00 ffff"/>
</Shape>
</Item>
</Selector>

Or mix drawable and color (but pay attention to the consistency of the four corners, because the four corners of the drawable may be arc-shaped ):Copy codeThe Code is 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>

:

Refer:
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.