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 and <
gradient android:startcolor = "#ff8c00"
Android:endcolor = "#FF FFFF "
Android:angle =" "/> <!--stroke--<
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 "/> </sha Pe>
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" > <shap E> <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> <s Olid 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:
Reference:
Http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6087.html
Use of Android:shape