Original articles, reproduced please specify the source http://www.cnblogs.com/baipengzhan/p/6284682.html
This article first lists the creation of a common state selector, and then follow the usual controls to list the specific uses of the state selector.
Color State Selector The color status selector is created in the Res/color directory, and the color directory does not usually exist, so we can create it ourselves.
The color state selector can only be used as a place to change colors, such as changing the text color, not as a background,
It is important to note that mistakes are taken as backgrounds.
The following is a color status selector, which can be used to create additional information:
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:color=" #f00 "android:state_checked=" true "/> <item android:color=" #f00 "Android:state_pressed=" true "/> <item android:color=" #f00 "android:state_selected=" true "/> < !--not selected color-- <item android:color= "#000"/></selector>
The color values here are hard-coded, not recommended, only for illustration.
This color state selector corresponds to four states, the first three of which correspond to the same color, and the last one to another color.
Four states are: checked (checked), pressed (pressed), checked (selected), and default state.
Let's look at the following using a small example:
<radiobutton android:id= "@+id/button_4" android:text= "button_4" android:button= "@null" Android:textcolor= "@color/radiobutton_color_selector" android:background= "@drawable/radiobutton_bg_ Selector " android:gravity=" center " android:layout_weight=" 1 " android:drawabletop=" @drawable/ Radiobutton_pic_selector " android:layout_width=" wrap_content " android:layout_height=" wrap_content "/ >
This is used when changing the text color in a radiobutton:
Android:textcolor= "@color/radiobutton_color_selector"
General Status Selector
This most commonly used state selector is created in the Res/drawable directory, which is present in the directory that does not need to be created.
The use of this state selector is much more versatile than the previous color state selector, such as changing the background image,
Change the background color, change the background shape and so on.
Let's create a state selector that changes the background color
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_selected=" true "android:drawable=" @color/radiobutton_bg_selected "/> <item android:state_checked= "true" android:drawable= "@color/radiobutton_bg_selected"/> <item Android:state_pressed= "true" android:drawable= "@color/radiobutton_bg_selected"/> <!--not selected - <item android:drawable= "@color/radiobutton_bg_normal"/></selector>
When creating it, be aware that this is done by changing the color of the Drawable property, not the Color property, as opposed to the colour state selector.
The Drawable property value cannot be set directly to a color value, such as #f00, but to encapsulate the color value in the values directory, colors.xml file.
The use of this selector is also very simple, as a background, see the Color status Selector example, set the background:
android:background= "@drawable/radiobutton_bg_selector"
Let's create a status selector that changes the background image
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_selected=" true "android:drawable=" @mipmap/ic_selected "/> <item Android:state_checked= "true" android:drawable= "@mipmap/ic_selected"/> <item android:state_pressed= " True "android:drawable=" @mipmap/ic_selected "/> <!--not selected- <item android:drawable=" @mipmap/ Ic_launcher "/></selector>
Note that this state selector also applies to the Android:drawable property, which appears instead of the color when the picture is set to be used.
However, when you enter the value of the Android:drawable property, the system may not be reminded (enter incomplete information, remind full information) situation, this time we have to manually enter.
The use of this state selector is also used in the above RadioButton example, in the text above the picture settings:
android:drawabletop= "@drawable/radiobutton_pic_selector"
Let's create a state selector that changes the background shape
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_pressed=" true "android:drawable=" @drawable/textview_pressed_shape "/> <item android:state_selected= "true" android:drawable= "@drawable/textview_pressed_shape"/> < Item android:state_checked= "True" android:drawable= "@drawable/textview_pressed_shape"/> <!--not selected-- > <item android:drawable= "@drawable/textview_normal_shape"/></selector>
The background shape changes when checked and unchecked, and the shape resource uses the shape file that you created, as shown in the following code:
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" android:shape= "Rectangle" > <!--The Four corners of the rectangle are set to rounded corners, with a radius of 5dp--> <corners android:radius= " 5DP/> <!--set the shape's color- <solid android:color= #f00/> <!--set the size of the shape-- < Size android:width= "80DP" android:height= "20DP"/></shape>
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" android:shape= Oval > <!--Set the color of the shape- <solid android:color= "#0f0"/> < !--set the size of the shape- <size android:width= "40DP" android:height= "20DP"/></shape>
The shape file is also created under the Res/drawable directory, which is not described in detail.
Note that the length width set in shape does not work, and no reason is currently found for future resolution.
Let's create a state selector that changes the gradient of the background
Here the difference between the state selector and the previous one is that the settings in the shape file are changed and the rest is the same, see the following code:
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" android:shape= "Rectangle" > <!--The Four corners of the rectangle are set to rounded corners, with a radius of 5dp--> <corners android:radius= " 5DP "/> <!--set gradient-- <gradient android:startcolor=" #00f "android:centercolor=" #0f0 "Android: Endcolor= "#f00"/></shape>
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" android:shape= "Rectangle" > <!--The Four corners of the rectangle are set to rounded corners, with a radius of 5dp--> <corners android:radius= " 5DP "/> <!--set gradient-- <gradient android:startcolor=" #f00 "android:centercolor=" #0f0 "Android: Endcolor= "#00f"/></shape>
OK, we will first summarize here, the last wordy, color state selector in the Res/color directory, this directory to be created manually, when used only as a color, can not be used as a background;
The General status selector is placed in the Res/drawable directory, this directory already exists, do not need to create, when used as a background, you can change the background color, shape, picture and so on.
Android Status Selector Usage Summary