Introduction
Selector The Chinese meaning selector, which is often used as a background for components in Android, has the advantage of eliminating the use of code to control the transformation of a component's different background colors or images in different states. Very convenient to use.
Definition of Selector
Selector is the state list (statelist), which is divided into two types, a color-selector and a drawable-selector.
Color-selector
Color-selector is a list of color states that can be used with color, and colors change as the component States. The location of the file is stored in
/res/color/filename.xml
The use in Java is: R.color.filename
The use in XML is:@[package]color/filename
Grammar
<?xmlversion="1.0" encoding="Utf-8"? ><selector xmlns:android="Http://schemas.android.com/apk/res/android" > <Item android:color="Hex_color"Color value, #RGB, $ARGB, #RRGGBB, #AARRGGBB android:state_pressed=["True" |"False"]//whether to touch android:state_focused=["true" | "false"]//whether to get focus android:state_selected=["true" | "false"]//whether the state android:state_checkable=["true" | "false"]//optional android:state_checked=["true" | "false"]//check if android:state_enabled=["true" | "false"]//is available android:state_window_focused=["true" | "false"/>//whether window focus </selector>
Example
Create a new Test_color_selector.xml under the/res/color/folder
<?xml version= "1.0" encoding= "Utf-8"?><SelectorXmlns:android="Http://schemas.android.com/apk/res/android" ><item android:state_pressed= "true" android:color="#ffff0000"/> <!--pressed-- > <item android:state_focused="true" android:color="#ff0000ff"/> <!-- Focused- <item android:color="#ff000000"/> <!--default--and </ selector>
Call:
<Button android:id="@+id/bt_about" style="@style/Button_style" android:layout_width="250dp" android:layout_height="50dp" android:layout_margin="5dp" android:textColor="@color/test_color_selector" android:text="@string/about" />
Effect: Notice the change of font color
Drawable-selector
Drawable-selector is a list of background chart states that can be used as a picture, and the background will vary depending on the state of the component. Files are stored in
/res/drawable/filename.xml
Called in Java: R.drawable.filename
Called in XML: @[package:]drawable/filename
Grammar
<?xmlversion="1.0" encoding="Utf-8"? ><selector xmlns:android="Http://schemas.android.com/apk/res/android" android:constantsize=["True" |"False"]//drawable is the size of the state change, true indicates a change, false means no transformation, the default is False android:dither=["True" |"False"]//when the bitmap is not the same as the pixel configuration of the screen (for example, a bitmap with a ARGB of 8888 and an RGB 555 screen), the color (dither) is self-recursive. When set to False, the non-recursive color. Default True android:variablepadding=["True" |"False"]>//the internal margin is changed, default false <Item android:drawable="@[package:]drawable/drawable_resource"//Picture Resources android:state_pressed=["True" |"False"]//whether to touch android:state_focused=["True" |"False"]//whether to get to focus android:state_hovered=["True" |"False"]//Whether the cursor is android:state_selected=["true" | "false"]//check if android:state_checkable=["true" | "false"]//can be checked android:state_checked=["true" | "false"]//check android:state_enabled=["true" | "false"]//is available android:state_activated=["true" | "false"]//whether to activate android:state_window_focused=["true" | "false"/>//whether the window gets focus </selector>
Example
<?xml version= "1.0" encoding= "Utf-8"?><SelectorXmlns:android="Http://schemas.android.com/apk/res/android" ><ItemAndroid:state_selected="True"android:drawable="@drawable/button_bg_press"/> <item android:state_focused="true" android:drawable="@drawable/button_bg_press"/> <item android:state_pressed="true " android:drawable=" @drawable/button_bg_press "/> <item android:drawable=" @ Drawable/button_bg_normol "/></selector>
Call:
<Button android:id="@+id/bt_about" style="@style/Button_style" android:background="@drawable/button_selector" android:layout_width="250dp" android:layout_height="50dp" android:layout_margin="5dp" android:textColor="@color/test_color_selector" android:text="@string/about" />
Effect: note Button background change
Summarize
Selector is a good way to achieve changes in the background and color of the view state, you can save a lot of logic code, after mastering can save a lot of Java code, but also to write some beautiful UI.
from:http://blog.csdn.net/wenwen091100304/article/details/49667293
"Go" use of selector in Android