<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android" > <Itemandroid:state_pressed= "true"android:drawable= "@drawable/bg_selected"> </Item> <Itemandroid:drawable= "@drawable/bg_unselect"> </Item></selector>
In the work, because the system gives the control is not beautiful, so the development of the leadership often want me to change the next interface, with art to replace the image of the system. At first, I just set a background image for the button and so on, although it is beautiful, but the interface looks rather rigid, such as the user clicked the button, the button did not react. I then add the control to the Ontouch listen to the event, press the background to change the color, let go and then restore the original color. But later found selector this weapon, is really overjoyed, no longer add Ontouch monitoring events, use up also convenient and flexible. I have to say that it is necessary to communicate technical experience with other developers, especially if I am solely responsible for the development of an app like myself.
Android selector to be configured under Drawable.
The properties that selector can set are:
android:state_pressed If it is true, displays the picture when it is clicked, and if False is not pressed, the default is displayed.
android:state_focusedIf true, gets the focus when it is displayed, or false does not get the focus display default.
android:state_selectedIf true, displays the picture when it is selected, or False if it is not selected.
android:state_checkableTrue to display the picture when the checkbox is available; False to display the checkbox when it is not available.
android:state_checkedTrue to display the picture when the checkbox is selected; False to display the checkbox when it is selected.
android:state_enabledTrue to display the picture when the component is available for use, false to display the picture when the component is not available.
android:state_window_focusedTrue to display the picture when this activity obtains the first focus; false when the picture is not displayed in front
These property values can also be superimposed, such as:
android:state_window_focused= "true" android:state_pressed= "true"
Indicates the background picture when the focus is acquired in non-touch mode and clicked.
In general, the button control only needs to be android:state_pressed . For example, the background setting for a button control is:
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android" > <Itemandroid:state_pressed= "true"android:drawable= "@drawable/bg_selected"> </Item> <Itemandroid:drawable= "@drawable/bg_unselect"> </Item></selector>
This indicates that when the button control is pressed, the background image is bg_selected, and the background image is bg_unselect in other cases (not being clicked or released after clicking).
Selector You can set the color of the text in addition to the background color of the component. For example, when a button control is pressed, the text color on the button changes. For example:
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android" > <Itemandroid:state_pressed= "true"Android:color= "#ffffff"></Item> <ItemAndroid:color= "#000000"></Item></selector>
That is, the button control has a color of black, but when it is pressed, the color turns white. When released, the color reverts to black.
The above can be set in code like this:
< Button Android:layout_width = "Match_parent" android:layout_height= "Wrap_content" android:background= "@drawable/ Btn_slector " android:text=" abc " android:textcolor=" @ Drawable/text_selector " />
In addition to selector, there is a shape that can be used to customize the graphical effect of the control, both of which can be used together.
Transferred from: http://blog.163.com/feng_yun_ju/blog/static/1781903932013416104215773
The use of selector in Android