androida--Background Selector Selector Usage summary (i)

Source: Internet
Author: User

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

[HTML]View Plain Copy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!--background picture without focus--
  4. <item android:drawable= "@drawable/handle_normal" android:state_enabled="true" android:state_window_focused="false"/>
  5. <!--touch mode when pressed background picture--
  6. <item android:drawable= "@drawable/handle_pressed" android:state_pressed="true"/ >
  7. <item android:drawable= "@drawable/handle_focused" android:state_enabled="true" android:state_focused="true"/>
  8. <item android:drawable= "@drawable/handle_normal" android:state_enabled="true"/ >
  9. <!--get focus when the picture background--
  10. <item android:drawable= "@drawable/handle_focused" android:state_focused="true" />
  11. <!--
  12. Android:state_selected is selected
  13. Android:state_focused is gaining focus
  14. Android:state_pressed is click
  15. Android:state_enabled is the set response event, which refers to all events
  16. -->
  17. </selector>

two . Using an 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

[HTML]View Plain Copy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_selected= "true" android:color="#FFF" />
  4. <item android:state_focused= "true" android:color="#FFF" />
  5. <item android:state_pressed= "true" android:color="#FFF" />
  6. <item android:color="#000" />
  7. </selector>
Button can also achieve more complex effects, such as gradients
Drawable/button_color.xml

[HTML]View Plain Copy
  1. <span style="color: #330033;" ><? XML version= "1.0" encoding="Utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">/
  3. <item android:state_pressed="true">//defines the pattern when the button is in the pressed state.
  4. <shape>
  5. <gradient android:startcolor="#8600ff" />
  6. <stroke android:width="2DP" android:color="#000000" />
  7. <corners android:radius="5DP" />
  8. <padding android:left="10DP" android:top="10DP"
  9. android:bottom="10DP" android:right="10DP"/>
  10. </shape>
  11. </Item>
  12. <item android:state_focused="true">//defines the shape when the button gets focus
  13. <shape>
  14. <gradient android:startcolor="#eac100"/>
  15. <stroke android:width="2DP" android:color= "#333333" color= "#ffffff"/ >
  16. <corners android:radius="8DP" />
  17. <padding android:left="10DP" android:top="10DP"
  18. android:bottom="10DP" android:right="10DP"/>
  19. </shape>
  20. </Item>
  21. </selector></span><span style="color: #ff0000;" > </span>

Finally, you need to include add two items to the button's XML file. For example main.xml file, need to add two Android in <button/> :focusable= "true" android:background= "@drawable/ Button_color "This associates the Drawale/button_color.xml file with this control.

androida--Background Selector Selector Usage summary (i)

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.