The use of Android--selector background selector (II.)

Source: Internet
Author: User

In the development of the application, in many cases to design the ListView or button control background, the following summarizes the use of Android selector:
1. Configure the Android selector in drawable.
Save the following XML file as your own named. xml file (for example, Item_bg.xml) and place the file in the drawable file, using the corresponding background image based on the state of the list item in the ListView when the system is used.

Copy CodeThe code is as follows:
<?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"/>
<!--background picture without focus--
<item android:state_window_focused= "false" android:drawable= "@drawable/pic1"/>
<!--get focus in non-touch mode and click on the background picture--
<item android:state_focused= "true" android:state_pressed= "true"
android:drawable= "@drawable/pic2"/>
<!--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"/>
<!--get focus when the picture background--
<item android:state_focused= "true" android:drawable= "@drawable/pic5"/>
</selector>


2. Use the above configuration file:
The first method is configured in the ListView configuration file with the following code: android:listselector= "@drawable/item_bg"
The second method is to add a property to the ListView item, with the following code: android:background= "@drawable/item_bg"
The third method is set in Java code with the following code: drawable drawable =getresources (). getdrawable (R.DRAWABLE.ITEM_BG);
Listview.setselector (drawable);
The above setting method shows that the effect is sometimes black, so you need to add the following code to the configuration file: android:cachecolorhint= "@android: Color/transparent" makes the background transparent.
Similarly, the button has some background effects, which are explained in the following properties:
Android:state_selected is the effect when the setting is selected
Android:state_focused is setting the effect of getting focus
Android:state_pressed is the effect of setting the click
Android:state_enabled is setting whether to respond to events
The following is a selector used to set the text state in the button, 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"/>

The use of Android--selector background selector (II.)

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.