Android: Custom control style (Selector)

Source: Internet
Author: User

Objective

In the process of developing an application, it is unavoidable to modify the style of the component, such as buttons, input boxes, etc. Now look at how to implement the customization of the style through Seletor. Let's look at the simple effect comparison

Overview implementation

First write this selector XML file, called Button_selector, placed under the Drawable folder, the approximate content is as follows

  

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <selectorxmlns:android= "Http://schemas.android.com/apk/res/android">3 4     <!--Press the status -5     <Itemandroid:state_pressed= "true">6         <Shape>7               <!--Border Color -8             <StrokeAndroid:width= "1dip"Android:color= "#728ea3" />9             <!--Background Color -Ten             <SolidAndroid:color= "#FFFFcc" />   One         </Shape> A          -     </Item> -     <!--Default State - the     <Item> -         <Shape> -             <!--Border Color - -             <StrokeAndroid:width= "1dip"Android:color= "#728ea3" /> +             <!--Background Color - -             <SolidAndroid:color= "#FFFFFF" />    +         </Shape>       A     </Item> at  - </selector>

Then set the Background property for the button: @drawable/button_selector, as shown below

  

< Button         Android:id = "@+id/btnselector"         android:layout_width= "Wrap_content"        android:layout_height= "Wrap_ Content "        android:background=" @drawable/button_selector "        android: Text= "Selector demo"/>

This way the custom style is successfully applied to the button.

Selector

First look at the official description:

You can describe the "state" list in an XML file. Each graphic are represented by an <item> element inside a single <selector> element. Each <item> uses various attributes to describe the state which it should is used as the graphic for the drawable .

It means that you can use an XML file to describe the status list. Under the unique selector node, use item to describe each state. Each item is identified by a different attribute to use for which property.

The following is a look at the specific properties of item

Android:drawable: Referencing a drawable resource

The Android:state_pressed:Boolean value, if set to True, represents the time when the object is being pressed

The Android:state_focused:Boolean value, if set to true, means that the object is used when the focus is obtained

The Android:state_hovered:Boolean value, if set to true, indicates when the object is in the hover state

The Android:state_selected:Boolean value, if set to true, indicates that the object is used when it is selected

The Android:state_checkable:Boolean value, if set to true, indicates when the object is allowed to be selected

The Android:state_checked:Boolean value, if set to true, indicates when the object is selected

The Android:state_enabled:Boolean value, if set to true, indicates when the object is available (in response to a touch or click event)

The Android:state_activated:Boolean value, if set to true, indicates when the object is being activated

A Android:state_window_focused:Boolean value that, if set to True, represents the time when the form gets focus

With the above attributes, you can flexibly customize the desired results, this example just shows the effect of the android:state_pressed state. The selector code is the button_selector mentioned above. The effect is as follows

  

Simple analysis

Take a look at the item on the press.

  

<!--Press the status -    <Itemandroid:state_pressed= "true">        <Shape>              <!--Border Color -            <StrokeAndroid:width= "1dip"Android:color= "#728ea3" />            <!--Background Color -            <SolidAndroid:color= "#FFFFcc" />          </Shape>            </Item>

The pressed effect shown is done through the shape label. This tag is used to specify the style of the background, because this focus on the use of selector, shape's usage is not too much to explain, just the code appears in the label to do a simple explanation.

Stroke: The style used to set the background border, you can define its width (width), color, whether it is a dashed line display, etc.

Color: Used to set the background color

Postscript

This text simply introduces the general usage of the next selector, the concrete flexible use can construct very powerful display effect.

Original address: http://www.cnblogs.com/luoaz/p/3764784.html

Full Demo:https://github.com/xiaoai-opensource/selector

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.