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:
Copy to clipboard XML/html code
-
- <Shape>
-
- <! -- Solid -->
-
- <Solid Android: Color="# Ff9d77"/>
- <! -- Gradient -->
-
- <Gradient
-
- Android: startcolor="# Ff8c00"
-
- Android: endcolor="# Ffffff"
-
- Android: Angle="270" />
- <! -- 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:
Copy to clipboard XML/html code
-
- <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:
Copy to clipboard XML/html code
-
- Main. xml:
-
- <Button
-
- Android: layout_width="Wrap_content"
- Android: layout_height="Wrap_content"
-
- Android: Text="Testshapebutton"
-
- Android: Background="@ Drawable/button_selector"
-
- />
-
- >
Button_selector.xml:
Copy to clipboard XML/html code
-
- <?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" />
- <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>
The running effect is as follows:
General status:
Get focus status:
press status: