1. background selector (IN res/drawable/, usage: android: background = "@ drawable/XXX ")
<? 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 ")
<? 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:
<? 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:
<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:
<? 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 ):
<? 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>
: