Android Chinese source file Shape usage Summary

Source: Internet
Author: User

Android Chinese source file Shape usage Summary

In Android development, we often use Shape to define various shapes. First, let's take a look at what labels under Shape mean:

Solid: Fill
Android: color specifies the fill color

Gradient: gradient
Android: startColor and android: endColor are the start and end colors respectively,

Android: 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". The radius android: gradientRadius = "50" must be specified for the radial gradient ".

Location corresponding to angle value

 

 

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 corners to different angles,

If five attributes are set at the same time, the Radius attribute is invalid.

Android: Radius = "20dp" sets the Radius of the four corners

Android: topLeftRadius = "20dp" sets the radius in the upper left corner.
Android: topRightRadius = "20dp" sets the radius in the upper right corner.
Android: bottomLeftRadius = "20dp" sets the radius in the lower right corner.
Android: bottomRightRadius = "20dp" sets the radius in the lower left corner.


Padding: interval
You can set the interval between the top, bottom, and left directions.

 

Here we will look at a simple example, ShapDemo, which defines two xml files in the drawable Folder:

The content of button_bg.xml is as follows:

  1. Android: width = "2dp"
  2. Android: bottomRightRadius = "5dp" android: topLeftRadius = "5dp"
  3. Android: topRightRadius = "5dp"/>
  4. Android: bottom = "10dp" android: right = "10dp" android: top = "10dp"/>
    The content of button_pressed_bg.xml is as follows:
    1. Android: gradientRadius = "50" android: startColor = "# ff8c00"
    2. Android: type = "radial"/>
    3. Android: dashGap = "3dp" android: width = "2dp" android: color = "# dcdcdc"/>
    4. Android: bottom = "10dp" android: right = "10dp" android: top = "10dp"/>
      The dash parameter is set in the stroke to change the graph edge to a dotted line.

      Add a button. xml file in the drawable folder. The content is as follows:



      1. The meaning of this file has been mentioned before. In normal (normal) circumstances, button_bg is displayed, and button_pressed_bg is displayed when it is press (pressed.

        Let's take a look at activity_main.xml in the layout directory:

        1. Android: layout_width = "match_parent" android: layout_height = "match_parent"> Android: layout_width = "wrap_content" android: background = "@ drawable/button" android: text = "TestShapeButton"/>
          Specify the background as the button. xml in the drawable folder.

          The program runs as follows:

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.