In order to achieve better user interaction, you often need to change the background color of the button and the font color of the textview to indicate the operational status of the user's current menu, android provides a method to define a selector so that programmers can configure the background and color values in advance. The specific method is as follows:
1. Button (imagebutton, imageview, including general view, viewgroup can all perform the same operation ):
1. First, add the background attribute for the button in your layout file, such as Android: Background = "@ drawable/btn_background"
2. Add the new XML file btn_background.xml in the drawable directory, that is, define some selector labels and specify the background image of the button in various states. The following code:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <selector xmlns: Android = "http://schemas.android.com/apk/res/android"> <br/> <item Android: state_focused = "true" <br/> Android: state_pressed = "true" <br/> Android: drawable = "@ drawable/button_focused"/> <br/> <item Android: state_focused = "false" <br/> Android: state_pressed = "true" <br/> Android: drawable = "@ drawable/button_defocused"/> <br/> <item Android: state_focused = "true" <br/> Android: drawable = "@ drawable/button_focused"/> <br/> <item Android: state_focused = "false" <br/> Android: drawable = "@ drawable/button_defocused"/> <br/> </selector>
3. Put the image used in the above XML file under drawable.
2. textview changes the font color. The difference between textview and button is that the textcolor attribute is changed, and the selector file is defined in the color directory:
1. Specify the textcolor attribute of textview in the layout file, for example, Android: textcolor = "@ color/textview_color ";
2. Add the new XML file textview_color.xml in the color directory and specify the color value of textview in various states. The following code:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <selector xmlns: Android = "http://schemas.android.com/apk/res/android"> <br/> <item Android: state_selected = "true" Android: color = "# fff"/> <br/> <item Android: state_focused = "true" Android: color = "# fff"/> <br/> <item Android: state_pressed = "true" Android: color = "# fff"/> <br/> <item Android: color = "#000"/> <br/> </selector> <br/>
3. OK.