In the daily development, there will inevitably be such a situation, for a button, TextView ... Setting a change in the color of a click may be a change in the background background, or it may be a change in the color of the font, simply say: By default, the font color or background is a color, and when pressed or clicked, it becomes another color.
There are some small pits in this, in order to avoid stepping on, hereby summarize.
(i), selector as drawable and color when using the differences and considerations
1, Background's selector
Conclusion:The Selector,item of background can only be used with the drawable attribute, not with the color attribute, and drawable must also be in the form of a reference
2, TextColor's selector
Conclusion: The Selector,item of TextColor cannot use the Drawable property, only the Color property, the value can be specified directly, or it can be specified by reference.
Summarize:
1, selector as a drawable resource, the item must be specified with the android:drawable attribute, and the resource is placed in the drawable directory
2. When selector is a color resource, item must be specified with the Android:color property, and the resource is placed in the color directory
Attention:
1,color resources can also be placed in the drawable directory, referring to use @drawable to reference, but do not recommend this, drawable resources and color resources best or separate;
2.The Android:drawable property can also refer to @color color values in addition to @drawable resources, but Android:color can only refer to @color;
3, thestatus of item is from top to bottom match, if match to a state of item, then it will use this item to specify the resource, instead of using the best matching rules; So set the default state, must write at the end, if written in front, then all the item will not work ( emphasis ).
Note the differences and considerations regarding the application of these, let's take a look at the properties under selector and the property description under item.
(b), selector properties
1, android:dither= "true"
boolean value . A value of "true" enables the jitter of bitmaps when the bitmap's pixel configuration differs from the screen (for example: ARGB 8,888-bit graph and RGB 565 screen), and the jitter is deactivated when the value is "false". The default value is true.
2, android:visible= "true"
Sets the initial visibility state, false by default
3, android:automirrored= "true"
Sets whether the image needs to be mirrored inverted, which is useful when the layout direction is RTL, that is, right-to-left layout, attributes added by API level (Android 4.4)
4, android:constantsize= "true"
boolean value .
The internal size of the plotted object report, which remains unchanged when the state changes, and the value is "true" (the size is the maximum of all States);
If the size varies according to the current state, the value is "false". The default value is False.
5, android:variablepadding= "true"
boolean value .
If the inner margin of the object can be drawn, the value is "true" depending on the current state of the selection;
If the padding should remain constant (based on the maximum padding for all states), the value is "false". Enabling this feature requires that you handle the execution layout when the state changes, which is usually not supported. The default value is False.
6, android:exitfadeduration= "1000"
Fade-out time, in milliseconds, when the state changes, when the old state disappears
7, android:enterfadeduration= "1000"
The fade-in time, in milliseconds, when the new state is displayed when the state changes
(c), the property under item
1, android:drawable
object resources can be drawn . necessary . References the drawing object resource to be inserted.
2, android:state_pressed
boolean value .
True if you should use this item when you press an object (such as Touch/tap a button);
The value is "false" if this item should be used in the default non-pressed state.
3, android:state_checked
boolean value . The value is "true" if this item should be used when the object is selected;
If you should use this item when the object is not selected, the value is "false".
4, Android:state_checkable
boolean value .
The value is "true" if this item should be used when the object can be selected;
If you should use this item when the object is not selected, the value is "false". (Only useful if the object can be converted between a checked and a non-selected widget.) )
5, android:state_selected
boolean value .
If you use a directed control to browse (for example, use the arrow keys to browse the list), this item should be used when the object is selected for the current user, and the value is "true";
If you should use this item when you do not select an object, the value is "false". android:state_focused
Use the selected state when the focus () is not sufficient (for example, the list view has focus but uses the arrow keys to select items in it).
6, android:state_enabled
boolean value .
If this item should be used when the object is enabled (capable of receiving touch/click events), the value is "true";
If this item should be used when the object is deactivated, the value is "false".
7, android:state_focused
boolean value .
The value is "true" if this item should be used when the object has input focus (for example, when the user chooses text input);
If this item should be used in the default non-focus state, the value is "false".
8, android:state_activated
boolean value .
The value is "true" if this item should be used when the object is activated as a continuous selection (for example, a list item that is selected before "highlight" in the continuous Navigation view);
If you should use this item when the object is not active, the value is "false". This entry is the newly introduced configuration for API level 11.
9, android:state_hovered
boolean value .
The value is "true" if the item should be used when the cursor hovers over the object;
If this item should be used in the default non-hover state, the value is "false".
Typically, this drawing object may be the same as a drawing object used for the "focus" state. This entry is the newly introduced configuration for API level 14.
10, android:state_window_focused
boolean value .
The value is "true" if this item should be used when the application window has focus (applied in the foreground);
The value is "false" if the application window should use this item when it has no focus (for example, a notification bar drop-down or a dialog box appears).
The following uncommon attributes are not yet clear what to use, to be supplemented.
11, Android:state_last
12, Android:state_first
13, Android:state_middle
14, Android:state_single
15, Android:state_drag_can_accept
16, Android:state_drag_hovered
17, android:state_accelerated
18, Android:state_active
In fact, in the actual development process, many of these properties are not used, in most cases, we only need to set the corresponding drawable or color resources according to the state of the control, and can use multiple states. For example:
At this point, selector and item under the Attributes and drawable, color properties of the attention to all the instructions!!! Thanks for reading.
Selector of Android Style