Background selector in android

Source: Internet
Author: User

/**
* Add a file in the res/drawable folder, which sets the image trigger status. You can set state_pressed, state_checked, state_pressed,
* State_selected, state_focused, state_enabled, and other statuses:
*
* Android: state_pressed
* If it is true, the image is displayed when it is clicked. If it is false, the image is displayed by default when it is not pressed.
*
* Android: state_focused
* True: it is displayed when the focus is obtained. false: The default value is displayed when the focus is not obtained.
*
* Android: state_selected
* True: the image is displayed when it is selected; false: The image is displayed when it is not selected.
*
* Android: state_checkable
* True: the image is displayed when the CheckBox is enabled; false: The image is displayed when the CheckBox is unavailable.
*
* Android: state_checked
* True: the image is displayed when the CheckBox is selected; false: The image is displayed when the CheckBox is selected.
*
* Android: state_enabled
* True: the image is displayed when the component is available; false: The image is displayed when the component is unavailable.
*
* Android: state_window_focused
* True: displays the image when the activity gets the focus at the beginning; false: displays the image when the focus is not at the beginning.
*
*/

1. Create an xml file at drawable/xxx. xml. Remember to put the relevant images in the same directory.
<? 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>

 

Ii. Use an xml file:
1. Method 1: Configure android: listSelector = "@ drawable/xxx in listview
Alternatively, you can add the property android: background = "@ drawable/xxx" to the listview item"
2. Method 2: Drawable drawable = getResources (). getDrawable (R. drawable. xxx );
ListView. setSelector (drawable); however, when the list is sometimes black, you must add android: cacheColorHint = "@ android: color/transparent" to make it transparent.
Related attributes:
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:
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"> // defines 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, in the main. xml file, you need to add two android: focusable = "true" android: background = "@ drawable/button_color" in <Button/>"

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.