Use of the selector background selector in Android

Source: Internet
Author: User

About the ListView and button to change the background of the original Android control, on the Internet to find some information is not very full, so now summarize the use of Android selector.

First of all, Android selector is configured in Drawable/xxx.xml.

First look at the status in the ListView:

Save the following XML file as your own named. xml file (for example, List_item_bg.xml), using the corresponding background image when the system is used, based on the state of the list item in the ListView.

Drawable/list_item_bg.xml

<?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>

Use some XML files: The first is to configure android:listselector= "@drawable/LIST_ITEM_BG" in the ListView

Either add the attribute android:background= "@drawable/list_item_bg" to the ListView item or use it in Java code: drawable drawable = Getresources (). getdrawable (R.DRAWABLE.LIST_ITEM_BG);

Listview.setselector (drawable); the same effect.

But this will appear the list sometimes black, need to add: android:cachecolorhint= "@android: Color/transparent"

Make it transparent.

Second, take a look at some of the background effects of button:

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.

Here's how to configure the text effect in the 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>

Button can also achieve more complex effects, such as gradients and so on.

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 pattern when the button 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" >

<!--defines the shape when the button gains 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 that contains the button. If it is main.xml file, we need to add two items in <button/>.

Android:focusable= "true"

android:backgroud= "@drawable/button_color"

This way, when you use the button, you can get rid of the yellow color background of the system, realize the personalized background, with the overall layout of the application is very useful AH

Use of the selector background 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.