[Sorting] details about selector and shape

Source: Internet
Author: User

Selector and shape (1)

Selector structure description:

<? XML version = "1.0" encoding = "UTF-8"?>

<Selector xmlns: Android = "http://schemas.android.com/apk/res/android">

<Item

Android: color = "hex_color"

Android: state_pressed = "True/false"

"True" indicates that the press is used (for example, when a button is pressed), and "false" indicates that the press is not used.

Android: state_focused = "True/false"

"True" indicates that the focus state is used (for example, the scroll ball/D-pad is used to focus the button); "false" indicates that the focus state is used.

Android: state_selected = "True/false"

"True" indicates that the selected state is used (for example, the tab is opened); "false" indicates that the selected state is used.

Android: state_active = "True/false"

"True" indicates that the status can be checked; "false" indicates that the status is not checked. (It is only useful for components that can be switched and checked-uncheckable .)

Android: state_checkable = "True/false"

"True" indicates that the selected status is used; "false" indicates that the selected status is not used.

Android: state_checked = "True/false"

"True" indicates that the selected status is used; "false" indicates that the selected status is not used.

Android: state_enabled = "True/false"

"True" indicates the available status (can receive touch/click events); "false" indicates the unavailable status.

Android: state_window_focused = "True/false"

"True" indicates that the application window is used when there is focus (the application is on the foreground); "false" indicates that the focus is used when there is no focus (for example, the notification bar is pulled down or displayed in the dialog box ).

/>

</Selector>

Structure Description of shape:

<Shape>

<! -- Solid -->

<Solid Android: color = "# ff9d77"/>

<! -- Gradient -->

<Gradient

Android: startcolor = "# ff8c00" <! -Start color -->

Android: endcolor = "# ffffff" <! -End color -->

Android: angle = "270" type = "parmname" text = "parmname"/>

<! -- Stroke -->

<Stroke

Android: width = "2dp"

Android: color = "# dcdcdc"/>

<! -- Rounded corner -->

<Corners

Android: radius = "2dp"/>

<Padding

Android: Left = "10dp"

Android: Top = "10dp"

Android: Right = "10dp"

Android: Bottom = "10dp"/>

</Shape>

 

The description of the above attributes is as follows:

Solid: solid, indicating Filling

Android: Color specifies the fill color

 

Gradient: Gradient

Android: startcolor and Android: endcolor are the start and end colors respectively. ndroid: angle is the gradient angle, which must be an integer multiple of 45.

In addition, the default gradient mode is Android: TYPE = "linear", that is, linear gradient. You can specify the gradient as a radial gradient, Android: TYPE = "RADIAL", and the radius must be specified for the radial gradient Android: gradientradius = "50 ".

 

Stroke: Stroke

Android: width = "2dp" stroke width, Android: Color stroke color.

We can also draw the stroke into a dotted line. The setting method is as follows:

Android: dashwidth = "5dp"

 

Android: dashgap = "3dp"

Android: dashwidth indicates the width of a horizontal line like '-', and Android: dashgap indicates the distance between them.

 

Corners: rounded corner

Android: radius is the Radian of an angle. The larger the value is, the closer the angle is.

We can also set the four angles to different angles:

<Corners

Android: toprightradius = "20dp" upper right corner

Android: bottomleftradius = "20dp" bottom right corner

Android: topleftradius = "1dp" upper left corner

Android: bottomrightradius = "0dp" lower left corner

/>

 

Note that bottomleftradius is in the lower right corner, rather than in the lower left corner. This is a bit depressing, but it does not affect usage. Remember to make a mistake.

Some people on the Internet said that setting 0dp is invalid, but I found it works during the test. I used 2.2. Maybe this problem has been fixed, if it is invalid, it can only be set to 1dp.

 

Padding: interval

This is not much to mention. It is often used in XML layout files.

 

The Code is as follows:

 

Main. xml:

<Button

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "testshapebutton"

Android: Background = "@ drawable/button_selector"

/>

>

 

Button_selector.xml:

<? XML version = "1.0" encoding = "UTF-8"?>

<Selector

Xmlns: Android = "http://schemas.android.com/apk/res/android">

<Item Android: state_pressed = "true">

<Shape>

<! -- Gradient -->

<Gradient

Android: startcolor = "# ff8c00"

Android: endcolor = "# ffffff"

Android: TYPE = "RADIAL"

Android: gradientradius = "50"/>

<! -- Stroke -->

<Stroke

Android: width = "2dp"

Android: color = "# dcdcdc"

Android: dashwidth = "5dp"

Android: dashgap = "3dp"/>

<! -- Rounded corner -->

<Corners

Android: radius = "2dp"/>

<Padding

Android: Left = "10dp"

Android: Top = "10dp"

Android: Right = "10dp"

Android: Bottom = "10dp"/>

</Shape>

</Item>

<Item Android: state_focused = "true">

<Shape>

<Gradient

Android: startcolor = "# ffc2b7"

Android: endcolor = "# ffc2b7"

Android: angle = "270" type = "parmname" text = "parmname"/>

<Stroke

Android: width = "2dp"

Android: color = "# dcdcdc"/>

<Corners

Android: radius = "2dp"/>

<Padding

Android: Left = "10dp"

Android: Top = "10dp"

Android: Right = "10dp"

Android: Bottom = "10dp"/>

</Shape>

</Item>

<Item>

<Shape>

<Solid Android: color = "# ff9d77"/>

<Stroke

Android: width = "2dp"

Android: color = "# fad3cf"/>

<Corners

Android: toprightradius = "5dp"

Android: bottomleftradius = "5dp"

Android: topleftradius = "0dp"

Android: bottomrightradius = "0dp"

/>

<Padding

Android: Left = "10dp"

Android: Top = "10dp"

Android: Right = "10dp"

Android: Bottom = "10dp"/>

</Shape>

</Item>

</Selector>

[Sorting] details about selector and shape

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.