Selector in Android

Source: Internet
Author: User

Android Background Selector Selector Usage Summary (2011-04-19 13:40:00) reproduced
Tags: &NBSP; android&NBSP; selector< Span class= "SG_TXTB" > &NBSP; Background selector &NBSP; It Category:  java/vb/ Android

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" >
<!--Background picture By default-
<item android:drawable= "@drawable/pic1"/>&NBSP;&NBSP;&NBSP;
<!--  Background picture without focus  -->  
  < Item android:state_window_focused= "false"  &NBSP;&NBSP;
         android:drawable= "@drawable/pic1"/> &NBSP;&NBSP;
<!--  the background picture when you get focus in non-touch mode and click   -->  
  <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); however, this can occur when the list is sometimes black and needs to be added: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" >       &NBSP;/&NBSP
<item android:state_pressed= "true" >// definition when button  in Span lang= "en-US" >pressed  State of the form.  
   < Span class= "Apple-converted-space" >             &NBSP;<SHAPE>

                  <gradient  android:startcolor=" #8600ff "/>&NBSP;

<stroke android:width= "2DP" android:color= "#000000"/&G T
<corners android:radius= "5DP"/>
<padding android:left= "10DP" android:top= "10DP" /c4>
android:bottom= "10DP" android:right= "10DP"/&G  T
</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"/>       < Span class= "Apple-converted-space" >   &NBSP;&NBSP;&NBSP;
                  </shape>  
 </item>
</selector> 
Finally, you need to include   button xml file added two items. For example main.xml  file, required in <button/ > Riga two android: Focusable= "true" android:background= "@drawable/button_color"

Selector in Android

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.