Steps to implement Android custom controls (ii)

Source: Internet
Author: User

The day before yesterday has written a blog about the implementation steps of the custom control, this is the detailed version of the attached code

First, we have to create a new Attrs.xml resource file that adds the additional properties we will customize the control to, which is the custom property of the custom control, with the following code:

<Resources>    <declare-styleablename= "TestView">        <attrname= "TextColor"format= "Color"></attr>        <attrname= "TextSize"format= "Dimension"></attr>        <attrname= "Imgbackground"format= "integer"></attr>        <attrname= "Topborder"format= "Boolean"></attr>        <attrname= "Bottomborder"format= "Boolean"></attr>        <attrname= "Leftborder"format= "Boolean"></attr>        <attrname= "Rightborder"format= "Boolean"></attr>    </declare-styleable></Resources>

Then we define a class of our own control, to inherit the corresponding control for example, if you want to customize a TextView control, you have to inherit the TextView class, and then define the variable that corresponds to your property in the class, and then get the value of the corresponding property through the Typedarray class. Then the main point, we have to use in the OnDraw function, the specific code is as follows:

 Public classMyViewextendsTextView {PrivatePaint Mtextpaint; PrivatePaint Mborderpaint; PrivateContext Mcontext; Private BooleanMtopborder; Private BooleanMbottomborder; Private BooleanMleftborder; Private BooleanMrightborder;  PublicMyView (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stubMcontext =context;        Initmyview (); //for our custom classes, we need to use a method called Obtainstyledattributes to get our definitions. TypedArray params =context.obtainstyledattributes (Attrs, R.styleable.testview); //gets the property value of the custom control.         intBackgroudid =Params.getresourceid (R.styleable.testview_imgbackground,0); if(Backgroudid! = 0) Setbackgroundresource (Backgroudid); intTextColor =Params.getcolor (R.styleable.testview_textcolor,0XFFFFFFFF);        SetTextColor (TextColor); floatTextSize = Params.getdimension (R.styleable.testview_textsize, 40);                Settextsize (textSize); Mtopborder= Params.getboolean (R.styleable.testview_topborder,true); Mleftborder= Params.getboolean (R.styleable.testview_leftborder,true); Mbottomborder= Params.getboolean (R.styleable.testview_bottomborder,true); Mrightborder= Params.getboolean (R.styleable.testview_rightborder,true); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); if(Mtopborder = =true) Canvas.drawline (0, 0, This. GetWidth ()-1, 0, Mborderpaint); if(Mrightborder = =true) Canvas.drawline ( This. GetWidth ()-1, 0, This. GetWidth ()-1,                     This. GetHeight ()-1, Mborderpaint); if(Mbottomborder = =true) Canvas.drawline ( This. GetWidth ()-1, This. GetHeight ()-1, 0,                     This. GetHeight ()-1, Mborderpaint); if(Mleftborder = =true) Canvas.drawline (0, This. GetHeight ()-1, 0, 0, Mborderpaint); }     Public voidInitmyview () {Mtextpaint=NewPaint ();        Mtextpaint.setcolor (Color.White); Mborderpaint=NewPaint ();    Mborderpaint.setcolor (Android.graphics.Color.BLACK); }     Public voidSetTextColor (intTextColor) {Mtextpaint.setcolor (0XFFAABBCC); }     Public voidSettextsize (floattextSize)    {mtextpaint.settextsize (textSize); }     Public voidSettopborder (BooleanMtopborder) {         This. Mtopborder =Mtopborder; }     Public voidSetbottomborder (BooleanMbottomborder) {         This. Mbottomborder =Mbottomborder; }     Public voidSetleftborder (BooleanMleftborder) {         This. Mleftborder =Mleftborder; }     Public voidSetrightborder (BooleanMrightborder) {         This. Mrightborder =Mrightborder; }     Public voidSetpaddings (floatPaddingleft,floatpaddingtop) {setpadding (int) Paddingleft, (int) paddingtop, 0, 0); }}

The final step is to use the controls you have defined in your activity's layout, using the package name of the package that contains the class of your custom control, plus the class name (when using the custom attribute, you have to remember the property you set)

< Mycontrol.myview                     Android:id = "@+id/textview_tue"                     android:layout_width= "@dimen/cell_width"                    android:layout_height= "40DP"                      android:layout_weight= "1"                    android:background= "@drawable /solid "                    android:text=" "/>

Note: The final note is that when using a custom control, you must precede it with a reference to your own property, as shown in the second line:

 <  linearlayout  xmlns:android  = "http://schemas.android.com/apk/res/android"   Xmlns:myview  = "Http://schemas.android.com/apk/res/com.zsxy_schedule"   Android:layout_width  =" Match_parent "   Android:layout_height  =" Match_parent "   Android:orientation  =" vertical "  >  

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.