Use "reprint" for Android shape

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:

Copy to clipboard XML/html code
  1. <Shape>
  2. <! -- Solid -->
  3. <Solid Android: Color="# Ff9d77"/>
  4. <! -- Gradient -->
  5. <Gradient
  6. Android: startcolor="# Ff8c00"
  7. Android: endcolor="# Ffffff"
  8. Android: Angle="270" />
  9. <! -- Stroke -->
  10. <Stroke
  11. Android: width="2dp"
  12. Android: Color="# Dcdcdc" />
  13. <! -- Rounded corner -->
  14. <Corners
  15. Android: radius="2dp" />
  16. <Padding
  17. Android: left="10dp"
  18. Android: Top="10dp"
  19. Android: Right="10dp"
  20. Android: Bottom="10dp" />
  21. </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
  1. <Corners
  2. Android: toprightradius="20dp"Upper right corner
  3. Android: bottomleftradius="20dp"Bottom right corner
  4. Android: topleftradius="1dp"Upper left corner
  5. Android: bottomrightradius="0dp"Lower left corner
  6. />

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
  1. Main. xml:
  2. <Button
  3. Android: layout_width="Wrap_content"
  4. Android: layout_height="Wrap_content"
  5. Android: Text="Testshapebutton"
  6. Android: Background="@ Drawable/button_selector"
  7. />
  8. >

Button_selector.xml:

Copy to clipboard XML/html code
  1. <?XML Version="1.0" Encoding="UTF-8"?>
  2. <Selector
  3. Xmlns: Android=Http://schemas.android.com/apk/res/android">
  4. <Item Android: state_pressed="True" >
  5. <Shape>
  6. <! -- Gradient -->
  7. <Gradient
  8. Android: startcolor="# Ff8c00"
  9. Android: endcolor="# Ffffff"
  10. Android: Type="RADIAL"
  11. Android: gradientradius="50" />
  12. <! -- Stroke -->
  13. <Stroke
  14. Android: width="2dp"
  15. Android: Color="# Dcdcdc"
  16. Android: dashwidth="5dp"
  17. Android: dashgap="3dp" />
  18. <! -- Rounded corner -->
  19. <Corners
  20. Android: radius="2dp" />
  21. <Padding
  22. Android: left="10dp"
  23. Android: Top="10dp"
  24. Android: Right="10dp"
  25. Android: Bottom="10dp" />
  26. </Shape>
  27. </Item>
  28. <Item Android: state_focused="True" >
  29. <Shape>
  30. <Gradient
  31. Android: startcolor="# Ffc2b7"
  32. Android: endcolor="# Ffc2b7"
  33. Android: Angle="270" />
  34. <Stroke
  35. Android: width="2dp"
  36. Android: Color="# Dcdcdc" />
  37. <Corners
  38. Android: radius="2dp" />
  39. <Padding
  40. Android: left="10dp"
  41. Android: Top="10dp"
  42. Android: Right="10dp"
  43. Android: Bottom="10dp" />
  44. </Shape>
  45. </Item>
  46. <Item>
  47. <Shape>
  48. <Solid Android: Color="# Ff9d77"/>
  49. <Stroke
  50. Android: width="2dp"
  51. Android: Color="# Fad3cf" />
  52. <Corners
  53. Android: toprightradius="5dp"
  54. Android: bottomleftradius="5dp"
  55. Android: topleftradius="0dp"
  56. Android: bottomrightradius="0dp"
  57. />
  58. <Padding
  59. Android: left="10dp"
  60. Android: Top="10dp"
  61. Android: Right="10dp"
  62. Android: Bottom="10dp" />
  63. </Shape>
  64. </Item>
  65. </Selector>

The running effect is as follows:
General status:

Get focus status:

press status:

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.