There is a scenario where a button or a picture (view) is clicked to change the style (which usually changes the background color). At this point, a solution might be a state selector. Here's how to implement the State selector:
Steps:
First, create a new folder like this: res/drawable
Second, create an XML file; named My_select.xml here (note Selecting a file of select type)
Third, the following code is written in it:
<?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/btn_green_pressed "/> <!--pressed press the color background picture. You can also create a new color file in the values directory to introduce color-- <item android:state_pressed= "false" android:drawable= "@drawable/btn _green_normal "/> <!--default background picture--></selector>
Note: Those two quoted pictures are their own casually put, they like to change.
Four, in the Layout file to button buttons set to this selector, the code is as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= ". Mainactivity "> <button android:layout_centerinparent=" true " android:background=" @drawable/BT _select " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:text=" Dot I change the color Oh "/></relativelayout>
Now run the program on the line, see:
As you can see, the default is the green picture background you set, pressed into a brownish yellow background.
If you set the color picker, the effect is as follows:
However, personal advice not to use color as a selector, for internal reasons, the current knowledge can not be explained. Because some view added color selector will be a problem, I hope to understand the specific principle. Know senior, can also leave a message to explain, in advance thank O (∩_∩) o
Android Beginner Tutorial: How to customize a status selector