Android App Shape usage summary!

In Android program development, we often use Shape to define various shapes.

First, let's know which labels are under the Shape and what they mean:

solid: fill android:color specifies the color of the fill

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 radial gradient, android:type="radial". Radial gradient needs to specify radius android:gradientRadius="50".

The position corresponding to the angle value is shown in the figure:

stroke: stroke android:width="2dp" stroke width, android:color stroke color. We can also make the stroke in the form of dotted line. The setting method is: android:dashWidth="5dp" android:dashGap="3dp" Where android:dashWidth represents the width of such a horizontal line '-', and android:dashGap represents the distance between them

corners: fillets android:radius is the radian of the angle. The larger the value, the rounder the angle. We can also set the four angles 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" set the radius of the upper left corner android:topRightRadius="20dp" set the radius of the upper right corner android:bottomLeftRadius="20dp" set 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 in the up, down, left and right directions

Here, let's take a simple example, ShapDemo. First define two xml files under the drawable folder:

button_bg.xml is as follows:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="" >

<!-- fill -->
<solid android:color="#Ff9d77 "/ > <! -- defines the color value of the fill -- >

<!-- Stroke  -->
    android:color="#fad3cf" /> <!--  Defines the width of the stroke and the color value of the stroke -- >

<!-- fillet -->
    android:topRightRadius="5dp" /> <!-- Sets the radius of the four corners -->

<!-- interval -->
    android:top="10dp" /> <!-- Set the interval in each direction -->


button_pressed_bg.xml is as follows:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="" >

<!-- Gradual change -->
    android:type="radial" />

<!-- Stroke  -->
    android:color="#dcdcdc" />

<!-- fillet -->
<corners android:radius="5dp" />

<!-- interval  -->
    android:top="10dp" />


Here's an illustration. The dash parameter is set in the stroke, so that the edge of the figure becomes a dotted line.

Add a selector button.xml file under the drawable folder, as follows:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="">

<item android:drawable="@drawable/button_pressed_bg" android:state_pressed="true"></item>
<item android:drawable="@drawable/button_bg"></item>


The meaning of this file has been mentioned before. Under normal conditions, button is displayed_ BG, button is displayed when pressed_ pressed_ bg.

Let's take another look at the activity under the layout directory_ Contents of main.xml:

<RelativeLayout xmlns:android="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" >

    android:text="TestShapeButton" />


Directly specify the background as button.xml under the drawable folder.

The screenshot of program operation is as follows:

Posted on Thu, 25 Nov 2021 18:14:26 -0500 by j115