Use of selector in Android

Source: Internet
Author: User

Introduction

Selector The Chinese meaning selector, which is often used as a background for components in Android, has the advantage of eliminating the use of code to control the transformation of a component's different background colors or images in different states. Very convenient to use.

Definition of Selector

Selector is the state list (statelist), which is divided into two types, a color-selector and a drawable-selector.

Color-selector

Color-selector is a list of color states that can be used with color, and colors change as the component States. The location of the file is stored in

/res/color/filename.xml

The use in Java is:R.color.filename

The use in XML is:@[package]color/filename

Grammar
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <Item Android:color= "Hex_color"//color value, #RGB, $ARGB, #RRGGBB, #AARRGGBBAndroid:state_pressed=["true" | "False"]//whether to touchAndroid:state_focused=["true" | "False"]//whether to get focusAndroid:state_selected=["true" | "False"]//whether it is StateAndroid:state_checkable=["true" | "False"]//is optionalAndroid:state_checked=["true" | "False"]//whether to selectAndroid:state_enabled=["true" | "False"]//is availableAndroid:state_window_focused=["true" | "False"]/>//whether the window is focused</selector>
Example

Create a new Test_color_selector.xml under the/res/color/folder

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_pressed=" true "          android:color=" #ffff0000 "/> <!-- Pressed--    <item android:state_focused= "true"          android:color= "#ff0000ff"/> <!-- Focused to    default --></selector>

Call:

<button        android:id= "@+id/bt_about"        style= "@style/button_style"        android:layout_width= "250DP"        android:layout_height= "50DP"        android:layout_margin= "5DP"        android:textcolor= "@color/test_color_ Selector "        android:text=" @string/about "/>

  

Drawable-selector

Drawable-selector is a list of background chart states that can be used as a picture, and the background will vary depending on the state of the component. Files are stored in

/res/drawable/filename.xml

Called in Java:R.drawable.filename

Called in XML: @[package:]drawable/filename

Grammar
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "android:constantsize=[" true "| "False"]//drawable whether the size of the state changes, true indicates a change, false means no transformation, the default is False android:dither=["true" | "False"]//when the bitmap is not the same as the pixel configuration of the screen (for example, a ARGB 8888 bitmap with an RGB 555 screen) will color itself (dither). When set to False, the non-recursive color. Default true android:variablepadding=["true" | "false"] >//whether the inner margin changes, default false <item android:drawable= "@[package:]drawable/drawable_resource"//Picture Resources Android:sta Te_pressed=["true" | "False"]//whether to touch android:state_focused=["true" | "False"]//whether to get to focus android:state_hovered=["true" | "False"]//whether the cursor has passed android:state_selected=["true" | "False"]//check if android:state_checkable=["true" | "False"]//can be checked android:state_checked=["true" | "False"]//check android:state_enabled=["true" | "False"]//is available android:state_activated=["true" | "False"]//whether to activate android:state_window_focused=["true" | "false"]/>//whether the window gets focus </selector>

  

Example
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_selected=" true "android:drawable=" @drawable/button_bg_press "/>    <item android:state_focused= "true" android:drawable= "@drawable/button_bg_press"/>    <item android: State_pressed= "true" android:drawable= "@drawable/button_bg_press"  />    <item android:drawable= "@ Drawable/button_bg_normol "  /></selector>

Call:

<button        android:id= "@+id/bt_about"        style= "@style/button_style"        android:background= "@drawable/ Button_selector "        android:layout_width=" 250DP "        android:layout_height=" 50DP "        android:layout_margin = "5DP"        android:textcolor= "@color/test_color_selector"        android:text= "@string/about"/>

  

Summarize

Selector is a good way to achieve changes in the background and color of the view state, you can save a lot of logic code, after mastering can save a lot of Java code, but also to write some beautiful UI.

Original http://blog.csdn.net/wenwen091100304/article/details/49667293

Use of selector in Android

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.