Use of shape in Android

Source: Internet
Author: User

Shape is often used in Android to define some of the display properties of a control, and today we see some shape's use, with a general understanding of shape and a little summary:

  1. First, create an XML file named Gradient_box under the Res/drawable folder:

    <?xml version= "1.0" encoding= "Utf-8"?>

    <!--

    A geometry defined in the shape drawable XML file, defined in the res/drawable/directory, with the file name filename filename called the resource ID of the access

    Accessed through R.drawable.filename in code, accessed through @[package:]drawable/filename in an XML file.

    -

    <!--

    android:shape=["Rectangle" | "Oval" | "Line" | "Ring"]

    Shape, which is the default rectangle, can be set to rectangle (rectangle), oval (oval), linear shape (line), Loop (ring)

    The following properties are available only at android:shape= "Ring:

    Android:innerradius size, radius of inner ring.

    Android:innerradiusratio floating-point type, which represents the radius of the inner ring at the width ratio of the ring.

    For example, if Android:innerradiusratio indicates that the inner ring radius is equal to the width of the ring divided by 5, the value can be overwritten and the default is 9.

    Android:thickness size, ring thickness

    Android:thicknessratio float, which represents the thickness of the ring at the width ratio of the ring, for example, if android:thicknessratio= "2",

    Then the thickness of the ring is equal to the width of the ring divided by 2. This value can be overridden by android:thickness, and the default value is 3.

    The Android:uselevel Boolean value, or False if it is used as a levellistdrawable using the value of true.

    -

    <shape

    Xmlns:android= "Http://schemas.android.com/apk/res/android"

    Android:shape= "Rectangle" >

    <!--

    Rounded Corners

    Android:radius integer radius

    Android:topleftradius integer upper-left corner radius

    Android:toprightradius integer upper right corner radius

    Android:bottomleftradius integer-left corner radius

    Android:bottomrightradius integer Right bottom corner radius

    -

    <corners

    Android:radius= "8DP"

    Android:topleftradius= "5DP"

    Android:toprightradius= "15DP"

    Android:bottomleftradius= "20DP"

    Android:bottomrightradius= "25DP"

    />

    <!--

    Fade color

    Android:startcolor Color value Start color

    Android:endcolor Color Value End Color

    Android:centercolor integer gradient middle color, which is the color between the start and end colors

    Android:angle integer Gradient angle (PS: When angle=0, the gradient is left-to-right. Then turn counterclockwise, when angle=90 is from bottom to top. Angle must be an integer multiple of 45)

    Android:type ["Linear" | "Radial" | "Sweep"] Gradient type (value: Linear, radial, sweep)

    Linear linear gradient, which is the default setting

    Radial a radioactive gradient, centered on the starting color.

    Sweep the gradient of the scanned line type.

    Android:uselevel ["True" | FALSE] set to TRUE if you want to use the Levellistdrawable object. Set to True no gradient. False with gradient color

    Android:gradientradius integral gradient radius. Used when android:type= "radial". Using android:type= "Radial" alone will cause an error.

    Android:centerx the relative position of the X-point coordinates of the integer gradient center

    Relative position of Y-point coordinates of Android:centery integral gradient Center

    -

    <gradient

    Android:startcolor= "#FFFF0000"

    Android:endcolor= "#80FF00FF"

    Android:angle= "45"

    />

    <!--

    Padding, that is, the distance between the content and the edge

    Android:left Integer left inner margin

    Android:top integral type upper inner margin

    Android:right Integral right inner margin

    Android:bottom integral type lower inner margin

    -

    <padding

    android:left= "10DP"

    android:top= "10DP"

    android:right= "10DP"

    Android:bottom= "10DP"

    />

    <!--

    Size sizes

    Android:width width of integral type

    Android:height height of integral type

    -

    <size

    Android:width= "600DP"

    />

    <!--

    Internal padding

    Android:color Color value Fill Color

    -

    <solid

    Android:color= "#ffff9d77"

    />

    <!--

    Strokes

    Android:width the width of an integer stroke

    Android:color Color Value Stroke color

    The Android:dashwidth integer indicates that the style of the stroke is the width of the dashed line, and the value is 0 o'clock, which is expressed as solid. A value greater than 0 is a dashed line.

    Android:dashgap integer indicates that a stroke is a dashed line, the interval between dashes is "----"

    -

    <stroke

    Android:width= "2DP"

    Android:color= "#dcdcdc"

    />

    </shape>

  2. Second, the file created in step one in the window layout file as the background of TextView:

    <?xml version= "1.0" encoding= "Utf-8"?>

    <linearlayout

    Xmlns:android= "Http://schemas.android.com/apk/res/android"

    Android:layout_width= "Fill_parent"

    android:layout_height= "Fill_parent" >

    <textview

    Android:layout_width= "Wrap_content"

    android:layout_height= "Wrap_content"

    android:text= "Shapetest"

    android:background= "@drawable/gradient_box"

    Android:textsize= "24.0DP"

    Android:textcolor= "@android: Color/black"

    />

    </LinearLayout>

  3. Third, the operation effect shows:


Let's look at the following code:
<shape>
<!--solid--
<solid android:color= "#ff9d77"/>
<!--gradients--
<gradient
Android:startcolor= "#ff8c00" <!--start Color--
Android:endcolor= "#FFFFFF"
Android:angle= "/>"
<!--strokes--
<stroke
Android:width= "2DP"
Android:color= "#dcdcdc"/>
<!--rounded corners--
<corners
android:radius= "2DP"/>
<padding
android:left= "10DP"
android:top= "10DP"
android:right= "10DP"
android:bottom= "10DP"/>
</shape>

Solid: Filled is the meaning of filling
ANDROID:COLOR Specifies the color of the fill

Gradient: Gradient
Android:startcolor and Android:endcolor are start and end colors respectively, Ndroid:angle is the 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 the gradient to radial gradient, android:type= "radial", radial gradient needs to specify the radius android:gradientradius= "50".

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 between them.

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 angles to different angles by:
<corners
android:toprightradius= "20DP" upper right corner
android:bottomleftradius= "20DP" lower right corner
android:topleftradius= "1DP" upper left corner
android:bottomrightradius= "0DP" lower left corner
/>
Here is a place to pay attention, Bottomleftradius is the lower right corner, not the lower left corner, this is a bit depressed, but do not affect the use, remember not to make a mistake on the line.
There are also online to see someone said set to 0DP invalid, but I found in the test is OK, I use 2.2, may fix the problem, if it is invalid, it can only be set to 1DP.

padding: Interval
This is not much to say, XML layout files are often used.


In general, the following is a specific example of use: using the background as a button in selector, define the normal state of the buttons, the state of the focus and the state of the press, 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:
<?xml version= "1.0" encoding= "Utf-8"?>
<selector
Xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:state_pressed= "true" >
<shape>
<!--gradients--
<gradient
Android:startcolor= "#ff8c00"
Android:endcolor= "#FFFFFF"
Android:type= "Radial" <!--radial Gradient--
Android:gradientradius= "/>"
<!--strokes--
<stroke
Android:width= "2DP"
Android:color= "#dcdcdc"
Android:dashwidth= "5DP" <!--dashed width-to-
android:dashgap= "3DP"/> <!--dashed gap--
<!--rounded corners--
<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= "/>"
<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>

Operating effects such as:

General Status:

Get Focus Status:

Press the Status:

Use of shape in Android

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.