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:
Let's look at the following code:
<shape>
<!--solid--
<solid android:color= "#ff9d77"/>
<!--gradients--
<gradient
Android:startcolor= "#ff8c00"
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"
Android:gradientradius= "/>"
<!--strokes--
<stroke
Android:width= "2DP"
Android:color= "#dcdcdc"
Android:dashwidth= "5DP"
android:dashgap= "3DP"/>
<!--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:
Properties in shape in Android