XML selector Change the click of the Interface status button and the normal background toggle color of the click and Normal toggle

Source: Internet
Author: User

A Create XML file, location:drawable/xxx.xml, in the same directory remember to put related pictures

<?xml version= "1.0" encoding= "Utf-8"?>    
<selector xmlns:android= "http://schemas.android.com/apk/res/android" >&NBSP;
<!--   Background image by default -->  
  <item android:drawable= " @drawable/pic1 "/>&NBSP;&NBSP;&NBSP;&NBSP;
<!--  background picture when there is no focus   -->  
  <item android:state_window_focused= " False "&NBSP;&NBSP;&NBSP;
        android:drawable=" @drawable/pic1 "/ >   
<!--  background picture when I get focus in non-touch mode and click  --> &NBSP;&NBSP
  <item android:state_focused= "true"  android:state_pressed= "true"    android:drawable= "@drawable/pic2"/>&NBSP;

<!--the background picture when clicked in touch mode --

<item android:state_focused= "false" android:state_pressed= "true" android:drawable= "@drawable/pic3"/>

<!--picture background When selected- -

<item android:state_selected= "true" android:drawable= "@drawable/pic4"/>

<!-- picture background when getting focus -

<item android:state_focused= "true" android:drawable= "@drawable/pic5"/>
</selector>
two . Use XML file:

1. Method One: Configure the android:listselector= "@drawable/xxx in the ListView
Or add properties to the ListView item android:background= "@drawable/xxx"

2. Method Two:drawable drawable = Getresources (). getdrawable (R.DRAWABLE.XXX);
Listview.setselector (drawable); but this will appear the list sometimes black, need to add:android:cachecolorhint= "@android: Color/transparent" Make it transparent.

Related properties:

Android:state_selected is selected
Android:state_focused is gaining focus
Android:state_pressed is click
Android:state_enabled is the set response event , which refers to all events

The selector effect of the button can also be set according to these states . You can also set selector to change The state of text in a button.


The following is the text effect in the configuration button:
Drawable/button_font.xml
<?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>
ButtonYou can also achieve more complex effects, such as gradients
Drawable/button_color.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >/
<item android:state_pressed= "True" >// defines the pattern 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 whenButton getsThe shape of 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>
Finally, you need to includeadd two items to the button's XML file. For example main.xml files, you need to add two Android in <button/> : focusable= "true" android:background= "@drawable/button_color"

XML selector Change the click of the Interface status button and the normal background toggle color of the click and Normal toggle

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.