Use of shape in Android

Source: Internet
Author: User

In Android, shape is often used to define some Display Properties of the control. Today I have read some shapes and have a general understanding of shape. I would like to make a summary:

First look at the followingCode:
<Shape>
<! -- Solid -->
<Solid Android: color = "# ff9d77"/>
<! -- Gradient -->
<Gradient
Android: startcolor = "# ff8c00"
Android: endcolor = "# ffffff"
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>
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.

In general, the following is a specific example: Used as the background of the button in selector, which defines the general status of the button, the obtained focus status, and the status when the button is pressed, respectively, 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:

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



Android: startcolor = "# ff8c00"
Android: endcolor = "# ffffff"
Android: type = "RADIAL"
Android: gradientradius = "50"/>

Android: width = "2dp"
Android: color = "# dcdcdc"
Android: dashwidth = "5dp"
Android: dashgap = "3dp"/>

Android: radius = "2dp"/>
Android: left = "10dp"
Android: Top = "10dp"
Android: Right = "10dp"
Android: bottom = "10dp"/>

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

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.