Introduction to selector background selector in android

Source: Internet
Author: User

Both listview and button need to change the background of the original android control, and some information on the Internet is not very comprehensive, so let's summarize the usage of android selector.

First, the android selector is configured in drawable/xxx. xml.

First look at the status in listview:

Save the following XML file as your own. xml file (for example, list_item_bg.xml). When using the system, you can use the corresponding background image based on the status of the list items 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">

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

Use some xml files: first, configure android: listSelector = "@ drawable/list_item_bg" in listview"

You can also add the property android: background = "@ drawable/list_item_bg" to the listview item, or use Drawable drawable = getResources () in java code (). getDrawable (R. drawable. list_item_bg );

ListView. setSelector (drawable); same effect.

However, sometimes the list is black. You need to 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 focal point

Android: state_pressed: Click

Android: state_enabled is used to set whether to respond to events. It refers to all events.

You can also set the selector effect of the button Based on these statuses. You can also set selector to change the text status in the button.

Configure the text effect in the button as follows:

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

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.