Android Development Series (18): XML Implementation of custom control styles in the drawable folder, androiddrawable
During Android development, we often need to change the style of the control, which can be changed by adding a background image, however, adding more background images will definitely increase the size of the APK file.
We can use the custom property shape.
Shape:
Gradient -- corresponding to the color gradient. Startcolor and endcolor are not mentioned much. Android: angle indicates the angle from which to change.
Solid -- fill.
Stroke-stroke.
Corners -- rounded corner.
Padding -- defines the distance from the content to the boundary.
Let's take a look at the effect:
Below we will use specific code to demonstrate:
First, create a new project, and then edit it in main. xml:
<Span style = "font-size: 18px;"> <? 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" android: orientation = "vertical"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_marginTop = "10dp" android: text = "demo text" android: textSize = "14pt" android: textColor = "#565656" android: background = "@ drawable/bg_border "/> <! -- Draw an image using android: drawableLeft --> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "demo text 2" android: textSize = "24pt" android: background = "@ drawable/bg_border2"/> </LinearLayout> </span>
You can see that the android: background attribute of the two textviews above uses the files @ drawable/bg_border and @ drawable/bg_border2.
Then, create the two Android xml files under the @ drawable Folder:
Bg_border.xml:
<Span style = "font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?> <Shape xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Fill: Set the background color --> <solid android: color = "# ffffff"/> <! -- Set borders --> <stroke android: width = "5px" android: color = "#873600" android: dashWidth = "5dip"/> <! -- Define the padding --> <padding android: top = "20dip"/> </shape> </span>
Bg_border2.xml:
<Span style = "font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?> <Shape xmlns: android = "http://schemas.android.com/apk/res/android" android: shape = "rectangle"> <! -- Specify the radius of the four rounded corners of the rounded rectangle --> <corners android: topLeftRadius = "20px" android: topRightRadius = "5px" android: bottomRightRadius = "20px" android: bottomLeftRadius = "5px"/> <! -- Specify the border line width and color --> <stroke android: width = "4px" android: color = "# f0f"/> <! -- Specifies that the gradient background color is used, and the gradient color of the sweep type is used from red to green to blue. --> <gradient android: startColor = "# f00" android: centerColor = "#0f0" android: endColor = "# 00f" android: type = "sweep"/> </shape> </span>
Let's take a look at our project directory View:
In Eclipse, how does one create an <selector> xml file under the drawable-mdpi folder?
Right-click the drawable-mdpi folder and choose new file from the shortcut menu. Enter myselector. xml to create a file.
I am working on the android project ui and importing images to the drawable folder. The mainxml under layout has also been changed, but there is an error.
There are many reasons for this error. Maybe the image name in your main. xml file is wrong. Maybe it's just that eclipse is a breeze. After you put the image in, refresh it first. If not, restart eclipse. In short, this is not a big problem and can be easily solved.