Android Development Series (18): Self-defined control style XML implementation in the Drawable directory

Source: Internet
Author: User

In the process of Android development, we often need to change the style of the control, can be used to add a background image of the way to change, but the background image is more and more certainly will make the apk file very large.

We can do this with our own definition of the attribute shape.


Shape

gradient--the corresponding color gradient. StartColor, EndColor will not say much. android:angle refers to the angle from which the change begins.

Solid--fill.

Stroke-- strokes.

Corners-rounded corners.

Padding-defines how far the content is from the boundary.


Let's look at the effect first:




Below we use the detailed code to demonstrate:

First, create a new project, and then we'll 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:backg round= "@drawable/bg_border"/> <!--draw a picture by Android:drawableleft--><textview android:layout_width= "fil L_parent "android:layout_height=" Wrap_content "android:text=" Demo text 2 "android:textsize=" 24pt "android:background=" @ Drawable/bg_border2 "/></LINEARLAYOUT></SPAN> 
can see, in the top of the two TextView android:background attribute, used to @drawable/bg_border and @drawable/bg_border2 these two files


We then create these two Android XML files below the @drawable directory:

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 the border--><stroke      Android:width= "5px"    android:color= "#873600"    android:dashwidth= "5dip"    /><!--define 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 ">    <!--radius of 4 rounded corners of the specified rounded rectangle--   <corners android: topleftradius= "20px"    android:toprightradius= "5px"    android:bottomrightradius= "20px"    android: bottomleftradius= "5px"/>   <!--Specifies the width and color of the border line--><stroke android:width= "4px" android:color= "#f0f"/ ><!--Specifies to use the gradient background color, use the sweep type of gradient color from red → green → blue--><gradient android:startcolor= "#f00"    android: Centercolor= "#0f0"    android:endcolor= "#00f"    android:type= "sweep"/></shape></span>


Let's take a look at our project folder view:







Android Development Series (18): Self-defined control style XML implementation in the Drawable directory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.