This article mainly introduces the combination of shape and selector in Android, this article directly gives the example code, the need for friends can refer to the following
One: In Android development, we often use shape to define a variety of shapes, and first we know what labels are underneath shape, and what they mean:
(1). Solid: Filling
Android:color Specify the color of the fill
(2). Gradient: Gradient
Android:startcolor and Android:endcolor, respectively, are the starting and ending colors,
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", which is a linear gradient,
You can specify a gradient to be a radial gradient, android:type= "radial", and a radial gradient that requires a radius of android:gradientradius= "50".
The angle value corresponds to the position shown below:
(3). Stroke: Strokes
Android:width= the width of the "2DP" stroke, android:color the stroke color.
We can also make the stroke in the form of a dotted line, set in the following way:
Android:dashwidth= "5DP"
android:dashgap= "3DP"
Where android:dashwidth means '-' the width of such a horizontal line, android:dashgap the distance between
(4). Corners: Rounded Corners
Android:radius is the angle of the radian, the greater the value of the more rounded corners.
We can also set the four corners to different angles and set five properties, the Radius property is invalid
android:radius= "20DP" sets the radius of Four Corners
android:topleftradius= "20DP" sets the radius of the upper-left corner
android:toprightradius= "20DP" sets the radius of the upper right corner
android:bottomleftradius= "20DP" sets the radius of the lower right corner
android:bottomrightradius= "20DP" sets the radius of the lower-left corner
(5). padding: Interval
You can set an interval of four directions up or down
Two: The code is as follows:
(1). Button_bg.xml as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27-28 |
<?xml version= "1.0" encoding= "Utf-8"?> <shape xmlns:android= "Http://schemas.android.com/apk/res/android" > <!--gradient--> <gradient android:endcolor= "#FFFFFF" android:gradientradius= "android:startcolor=" # Ff8c00 "android:type=" Radial/> <!--stroke--> <stroke android:dashgap= "3DP" android:dashwidth= "5DP" Android: Width= "2DP" android:color= "#dcdcdc"/> <!--fillet--> <corners android:radius= "5DP"/> <!--interval--> & Lt;padding android:bottom= "10DP" android:left= "10DP" android:right= "10DP" android:top= "10DP"/> </shape> |
(2). Shape_image.xml as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?xml version= "1.0" encoding= "Utf-8"?> <shape xmlns:android= "Http://schemas.android.com/apk/res/android" > <!--fill--> <solid android:color= "#ff9d77"/> <!--define the color value of the fill--> <!--stroke--> <stroke Android:width= "2DP" android:color= "#fad3cf"/> <!--defines the color value of the stroke's width and stroke--> <!--rounded corners--> <corners android: bottomleftradius= "5DP" android:bottomrightradius= "5DP" android:topleftradius= "5DP" android:toprightradius= "5DP"/ > <!--set four-corner radius--> <!--interval--> <padding android:bottom= "10DP" android:left= "10DP" android:right= "10 DP "android:top=" 10DP/> <!--sets the interval of each direction--> </shape> |
(3). Button.xml as follows:
?
1 2 3 4 5 6 7 |
<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android= "http://schemas.android.com/apk/res/" Android > <item android:drawable= "@drawable/button_pressed_bg" android:state_pressed= "true" ></item > <item android:drawable= "@drawable/shape_image" ></item> </selector> |
QQ Space Sina micro bo Tencent micro Bo Sohu Weibo renren everyone open