Introduction to selector background selector in Android

Source: Internet
Author: User

concerning listview and button, we need to change the background of the original Android control. I found some information on the Internet, which is not comprehensive. So I will summarize the usage of Android selector.
first, the android selector is configured in drawable/xxx. xml.
first check the status in listview:
Save the following XML file as your own name. XML files (such as list_item_bg.xml) use the corresponding background image based on the status of the list items in the listview during system use.
drawable/list_item_bg.xml

 
   
   
    
    
    
    
    
    
    
    
    
    
    
    
   

use some XML files: first, configure android in listview: listselector = "@ drawable/ list_item_bg "

or add the property Android: background = "@ drawable/ list_item_bg , or use drawable = getresources () in Java Code (). getdrawable (R. drawable. list_item_bg );
listview. setselector (drawable); same effect.
however, if the list is sometimes black, you must add: Android: cachecolorhint =" @ Android: color/transparent "
make it transparent.
next, let's take a look at some background Effects of the button:
Android: state_selected is selected
Android: state_focused is the focus
Android: state_pressed is the click
Android: state_enabled is the set whether to respond to the event, all events
you can set the selector effect of the button Based on these statuses. You can also set selector to change the text status in the button.
below is the text effect in the button configuration:
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>

Button can also achieve more complex effects, such as gradient.
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 form of a button when it 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 obtains the 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 add two items to the XML file containing the button. For example, the main. xml file,We need to add two items in <button/>.

Android: focusable = "true"
Android: backgroud = "@ drawable/button_color"
In this way, when you use the button, you can get rid of the yellow background that comes with the system. It is very useful to achieve a personalized background and cooperate with the overall layout of the application.

Transferred from:Http://blog.csdn.net/jrcisme/article/details/6444653

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.