Android Custom Controls

Source: Internet
Author: User

In the Android system, all controls and layouts are sub-classes or indirect subclasses of view, such as button inheritance Textview,textview inherits view.

If we want to define a control, we must inherit the subclass of view or view.

If you want to extend an existing control, inherit the control and modify the internal implementation.

If you want to create a new control, inherit the view.

For example, the Android control architecture

First of all, let's take a look at how to define our own textview, as an example of expanding TextView.

Customizing a class, inheriting TextView

1  Public classMytextviewextendsTextView {2      PublicMytextview (context context, AttributeSet Attrs) {//The system invokes the control when it is created based on XML3         Super(context, attrs);4     }5      PublicMytextview (Context context) {//When you create an object using your own code, you call6         Super(context);7     }8}

In general, we have to configure the space in the XML layout file, so we generally use the top of the constructor.

Then rewrite the OnDraw method to draw the interface.

1 @Override2     protected voidOnDraw (canvas canvas) {3Paint Paint_blue =NewPaint ();//Brushes4Paint_blue.setcolor (Color.Blue);//Blue5Paint_blue.setstyle (Style.stroke);//Hollow, FILL (solid)6Paint_blue.setstrokewidth (10);//line width7Canvas.drawcircle (110,150,60,paint_blue);//Drawing8     9Paint Paint_text =NewPaint ();//Draw a StringTen Paint_text.setcolor (color.blue); OnePaint_text.settextsize (40); ACanvas.drawtext ("Sihai Special label", 215, 190, paint_text); -         Super. OnDraw (canvas); -}

The following figure is the TextView we draw, in addition to the text set, there is a circle and the blue text

If we want to set the property to the Android system with the control, we need to set the following in Values/attrs.xml

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <declare-styleablename= "CustomView">        <attrname= "radius"format= "integer" />        <attrname= "TextColor"format= "Color" />    </declare-styleable></Resources>

Defines 2 properties,

    1. RADIUS, constrained to integral type
    2. TextColor, constrained to color

These properties can be used in the construction method of the control, as follows

 PublicMytextview (Context context, AttributeSet attrs) {Super(context, attrs); //Get Property CollectionTypedArray A =context.obtainstyledattributes (Attrs,r.styleable.customview); //Gets the RADIUS attribute in the CustomView, if not, the defaultRadius = A.getint (r.styleable.customview_radius,10); //gets the TextColor property in the CustomView, if not, the default bluecolor =A.getcolor (R.styleable.customview_textcolor, Color.Blue); //after retrieving the Typedarray, the Typedarray can no longer be used after this function is called. a.recycle ();}

Once obtained, it can be used in the OnDraw method.

As follows

@Overrideprotected voidOnDraw (canvas canvas) {Paint Paint_blue=NewPaint ();//BrushesPaint_blue.setcolor (color);//use a custom colorPaint_blue.setstyle (Style.stroke);//HollowPaint_blue.setstrokewidth (10);//line widthCanvas.drawcircle (110,150,radius,paint_blue);//draw a circle, using a custom radiusPaint Paint_text =NewPaint ();//Draw a StringPaint_text.setcolor (Color.Blue); Paint_text.settextsize (40); Canvas.drawtext ("Sihai Special label", 215, 190, Paint_text); Super. OnDraw (canvas); }

These properties can be set when we define the control,

Before this, you need to add the following code to the root layout where toolbar is the prefix that defines the property

Xmlns:toolbar=http://schemas.android.com/apk/res-auto

Then we can set our own controls and properties.

< Com.ccshxt.shxt.MyTextView         Android:layout_width = "Match_parent"         android:layout_height= "Match_parent"        android:layout_below= "@+id/ Message "        android:text=" Plain text "        toolbar:radius=" Max "         toolbar:textcolor= "#FF0000"/>

The name of the control here is the class we define, and be careful not to forget the package name.

Effects such as

Android Custom Controls

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.