Summary of usage based on the Android background selector selector _android

Source: Internet
Author: User
A Create an XML file, location: Drawable/xxx.xml, under the same directory remember to put related pictures
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<!--background picture without focus-->
<item android:drawable= "@drawable/handle_normal" android:state_enabled= "true" android:state_window_focused= " False "/>
<!--touch Mode press the background picture-->
<item android:drawable= "@drawable/handle_pressed" android:state_pressed= "true"/>
<item android:drawable= "@drawable/handle_focused" android:state_enabled= "true" android:state_focused= "true"/ >
<item android:drawable= "@drawable/handle_normal" android:state_enabled= "true"/>
<!--picture background When the focus is obtained-->
<item android:drawable= "@drawable/handle_focused" android:state_focused= "true"/>
<!--
Android:state_selected is selected
Android:state_focused is getting the focus
Android:state_pressed is clicked
Android:state_enabled is setting whether to respond to events, which means all events
-->
</selector>

two . To use an XML file:
1. Method One:InListviewConfigured inAndroid:listselector= "@drawable/xxx
or inListviewOfItemAdd attribute inAndroid:background= "@drawable/xxx"

2. Method Two: drawable drawable = Getresources (). getdrawable (r.drawable.  XXX);
Listview.setselector (drawable); But this can happen when the list is sometimes black, and you need to add:android:cachecolorhint= "@android: Color/transparent" make it transparent.
Related properties:

Android:state_selected is selected
Android:state_focused is getting the focus
Android:state_pressed is clicked
Android:state_enabled is setting whether to respond to events, which means all events

The selector effect of the button can also be set based on these states . You can also set selector to change The state of text in the button.
The following is the text effect in the Configure button:
Drawable/button_font.xml

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:state_selected= "true" android:color= "#FFF"/>
<item android:state_focused= "true" android:color= "#FFF"/>
<item android:state_pressed= "true" android:color= "#FFF"/>
<item android:color= "#000"/>
</selector>

A button can also achieve more complex effects, such as a gradient
Drawable/button_color.xml
Copy Code code as follows:

<span style= "COLOR: #330033" ><?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >/
<item android:state_pressed= "True" >//define the form when the button is in the pressed state.
<shape>
<gradient android:startcolor= "#8600ff"/>
<stroke android:width= "2DP" android:color= "#000000"/>
<corners android:radius= "5DP"/>
<padding android:left= "10DP" android:top= "10DP"
Android:bottom= "10DP" android:right= "10DP"/>
</shape>
</item>
<item android:state_focused= "True" >//define the form when the button gets focus
<shape>
<gradient android:startcolor= "#eac100"/>
<stroke android:width= "2DP" android:color= "#333333" color= "#ffffff"/>
<corners android:radius= "8DP"/>
<padding android:left= "10DP" android:top= "10DP"
Android:bottom= "10DP" android:right= "10DP"/>
</shape>
</item>
</selector></span><span style= "COLOR: #ff0000" > </SPAN>

Finally, you need to add two items to the XML file that contains the button. For example Main.xml file, you need to add two android:focusable= "true" android:background= "@drawable/button_color" in <button/> This associates the Drawale/button_color.xml file with the control.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.