Android selector background selector

Source: Internet
Author: User

In the development of applications, the background of listview or button control should be designed in many cases. The following summarizes the usage of android selector:
1. Configure Android selector in drawable.
Save the following XML file as your own name. xml file (such as item_bg.xml), and place the file in the drawable file. When used by the system, the corresponding background image is used according to the status of the list items in the ListView.

Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
<! -- Default background image -->
<Item android: drawable = "@ drawable/pic1"/>
<! -- Background image without focus -->
<Item android: state_window_focused = "false" android: drawable = "@ drawable/pic1"/>
<! -- Background image when the focus is obtained in non-touch mode and clicked -->
<Item android: state_focused = "true" android: state_pressed = "true"
Android: drawable = "@ drawable/pic2"/>
<! -- Background image when clicked in touch mode -->
<Item android: state_focused = "false" android: state_pressed = "true"
Android: drawable = "@ drawable/pic3"/>
<! -- Background of the selected image -->
<Item android: state_selected = "true" android: drawable = "@ drawable/pic4"/>
<! -- Background of the image when the focus is obtained -->
<Item android: state_focused = "true" android: drawable = "@ drawable/pic5"/>
</Selector>

2. Use the preceding configuration file:
The first method is to configure in the listview configuration file. The Code is as follows: android: listSelector = "@ drawable/item_bg"
The second method is to add attributes to the listview item. The Code is as follows: android: background = "@ drawable/item_bg"
The third method is to set it in java code. The Code is as follows: Drawable drawable = getResources (). getDrawable (R. drawable. item_bg );
ListView. setSelector (drawable );
The above setting method sometimes shows a black effect, so you need to add the following code to the configuration file: android: cacheColorHint = "@ android: color/transparent" to make the background transparent.
Similarly, the Button has some background effects, which are explained as follows:
Android: state_selected is used to set the selected effect.
Android: state_focused is set to get the focus Effect
Android: state_pressed: Set the click Effect
Android: state_enabled is used to set whether to respond to events.
The following is a selector used to set the text status in the button. The Code is as follows:Copy codeThe Code is as follows: <? Xmlversion = "1.0" encoding = "UTF-8"?>
<Selectorxmlns: android = "http://schemas.android.com/apk/res/android">
<Itemandroid: state_selected = "true" android: color = "# FFF"/>
<Itemandroid: state_focused = "true" android: color = "# FFF"/>
<Itemandroid: state_pressed = "true" android: color = "# FFF"/>
<Itemandroid: color = "#000"/>
</Selector>

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.