Android Custom Control-textview

Source: Internet
Author: User

Many times the system comes with a view that doesn't meet the requirements of the design, and you need to customize the view control. Custom view first implements a class that inherits from view. Add the constructor method of the class, override the parent class's methods, such as OnDraw, (onmeasure), and so on. If a custom view has its own properties, you need to create the Attrs.xml file under values, define the properties, and modify the code.

A simple example:

• Create a new MyView class, inherit from TextView, and add a constructor method:

 PackageCom.example.custview;ImportAndroid.content.Context;ImportAndroid.content.res.TypedArray;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.util.AttributeSet;ImportAndroid.widget.TextView; Public classMyViewextendsTextView {Paint mpaint=NULL;  PublicMyView (Context context) {Super(context); //TODO auto-generated Constructor stub    }     PublicMyView (Context ctx, AttributeSet attrs) {Super(CTX, attrs); Mpaint=NewPaint (); TypedArray Array=ctx. Obtainstyledattributes (Attrs, R.styleable.myview); intTextColor =array. GetColor (R.styleable.myview_textcolor,0XFF00FF00);//provide default values, place unspecified        floatTextSize = Array.getdimension (r.styleable.myview_textsize, 36);        Mpaint.setcolor (TextColor);        Mpaint.settextsize (textSize);    Array.recycle (); } @Override Public voidDraw (canvas canvas) {//TODO auto-generated Method Stub        Super. Draw (canvas); if(Mpaint = =NULL) {Mpaint=NewPaint ();        } mpaint.setcolor (color.red);        Mpaint.setstrokecap (Paint.Cap.ROUND); Mpaint.setstrokewidth (3); Canvas.drawrect (0, 0, 20, 20, Mpaint); }}

Then use it in the main layout:

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Xmlns:my= "Http://schemas.android.com/tools/res/com.example.custview"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context=". Mainactivity " >    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_centerhorizontal= "true"android:layout_centervertical= "true"Android:text= "@string/hello_world" /><Com.example.custview.MyViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"My:textcolor= "#FFFFFFFF"my:textsize= "33DP" /> </Relativelayout>

You can see the following results in the graphical layout:

can be completed. Run results

Note that since we used the layout above, that is, the custom control is configured in XML, it is important to add a construction method (which is not required if you do not have space settings in the layout):

 PublicMyView (Context context,attributeset attrs) {Super(context, attrs); } At least write the above contents in the XML file. Where com.example.custview.MyView this sentence is the class represented by the control that needs to be displayed. This class is definitely inherited from the view of the custom class (in fact, let us write, this is nonsense, can be in the project directly source code add Xxxx.java, can also be in the Libs directory of their newly added jar package inside. In the case of a class inside a jar package, the path is the path to the class inside the jar package. After completing the two steps above, you can instantiate the layout file in your code @override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); //Setcontentview (New MyView (this));displays the same effect. Here's how to implement the property settings for a custom view. To implement custom view property settings, you need to: • Create a Attrs.xml file in the values directory, add a property content • Add a new namespace xmlns to the layout file, and then you can use the namespace to set properties for your custom settings • After you set the properties, you will, of course, process them. Processing in a constructed method in a custom view class follow these three steps to illustrate an example of adding a Attrs.xml file first, in defining a property<?xml version= "1.0" encoding= "Utf-8"?> <resources> <declare-styleable name= "MyView" > <attr na Me= "TextColor" format= "color"/> <attr name= "textSize" format= "Dimension"/> </declare-styleable> &lt ;/resources> PublicMyView (Context context,attributeset attrs) {Super(context, attrs); Mpaint=NewPaint (); //Typedarray is an array for storing properties obtained by Context.obtainstyledattributes//Be sure to call the Recycle method after use is complete//The name of the property is the name in Styleable + "_" + Property name//TypedArray array = context.obtainstyledattributes (Attrs, r.styleable.myview);TypedArray Array=context.obtainstyledattributes (Attrs, R.styleable.myview); intTextColor = Array.getcolor (R.styleable.myview_textcolor, 0xff00ff00);//provide default values, place unspecified        floatTextSize = Array.getdimension (r.styleable.myview_textsize, 36);          Mpaint.setcolor (TextColor);          Mpaint.settextsize (textSize); Array.recycle (); //Be sure to call, otherwise this time the setting will affect the next use          } 

Android Custom Control-textview

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.