A summary of shape usage in Android

Source: Internet
Author: User
Tags dashed line

In the development of Android programs, we often use shape to define a variety of shapes, first of all we understand what the shape of the label below, it means:

Solid: Fill
ANDROID:COLOR Specifies the color of the fill

Gradient: Gradient
Android:startcolor and Android:endcolor are the starting and ending colors, respectively,

Android:angle is a gradient angle and must be an integer multiple of 45.
In addition, the default mode of the gradient is android:type= "linear", that is, linear gradient,

You can specify a gradient as a radial gradient, android:type= "radial", and a radial gradient that requires a radius of android:gradientradius= "50".

Angle value corresponding to the location

Stroke: Stroke
Android:width= the width of the "2DP" stroke, android:color the color of the stroke.
We can also make the stroke into a dashed form, set in the following way:
Android:dashwidth= "5DP"
android:dashgap= "3DP"
Where android:dashwidth represents the width of a horizontal line such as '-', android:dashgap represents the distance separated by

Corners: Rounded Corners
The Android:radius is the radian of the angle, and the larger the value the greater the rounded.
We can also set the four corners to different angles,

The RADIUS attribute is not valid if you set five properties at the same time

android:radius= "20DP" sets the radius of Four Corners

android:topleftradius= "20DP" sets the radius of the upper-left corner
Android:toprightradius= "20DP" set the radius of the upper-right corner
android:bottomleftradius= "20DP" setting the radius of the lower right corner
Android:bottomrightradius= "20DP" set the radius of the lower left corner

padding: Interval
You can set the interval between the top and left four directions

Here we look at a simple small example, Shapdemo, define two XML files under the Drawable folder first:

The contents of Button_bg.xml are as follows:

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <!---filled
  4. <solid android:color="#ff9d77" /> <!--defines the color value of the fill-
  5. <!--strokes --
  6. <stroke
  7. android:width="2DP"
  8. android:color="#fad3cf" /> <!--defines the width of the stroke and the color value of the stroke-
  9. <!--rounded corners --
  10. <Corners
  11. android:bottomleftradius="5DP"
  12. android:bottomrightradius="5DP"
  13. android:topleftradius="5DP"
  14. android:toprightradius="5DP" /> <!--set the radius of Four Corners--
  15. <!--interval --
  16. <padding
  17. android:bottom="10DP"
  18. android:left="10DP"
  19. android:right="10DP"
  20. android:top="10DP" /> <!--set the intervals in each direction--
  21. </shape>


The contents of Button_pressed_bg.xml are as follows:

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <!--gradients --
  4. <gradient
  5. android:endcolor="#FFFFFF"
  6. android:gradientradius="
  7. android:startcolor="#ff8c00"
  8. android:type="radial" />
  9. <!--strokes --
  10. <stroke
  11. android:dashgap="3DP"
  12. android:dashwidth="5DP"
  13. android:width="2DP"
  14. android:color="#dcdcdc" />
  15. <!--rounded corners --
  16. <corners android:radius="5DP" />
  17. <!--interval --
  18. <padding
  19. android:bottom="10DP"
  20. android:left="10DP"
  21. android:right="10DP"
  22. android:top="10DP" />
  23. </shape>


Here is a point where the dash parameter is set in the stroke, making the edges of the graphic a dashed line

Under the Drawable folder, add a Button.xml file that reads as follows:

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable= "@drawable/button_pressed_bg" android:state_pressed="true" ></Item>
  4. <item android:drawable="@drawable/button_bg"></Item>
  5. </selector>


The meaning of this file has been mentioned before, normal (normal) case shows BUTTON_BG, by press (pressed) the case is displayed BUTTON_PRESSED_BG.

Let's take a look at the contents of the Activity_main.xml in the layout directory:

[HTML]View Plaincopy
  1. <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="Http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. <Button
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:background="@drawable/button"
  9. android:text="Testshapebutton" />
  10. </relativelayout>


Specify background directly as Button.xml under the Drawable folder.

The program runs as follows:

A summary of shape usage in Android

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.