Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/47082241
In this blog post, we'll show you how to customize the controls in Android in the simplest way, let's take a custom TextView example to show you how to customize the controls in Android.
First, let's briefly talk about the principle of custom controls in Android: Create a class, inherit the control class that you want to customize, and override the related methods of the parent class. The principle is finished, is it very simple? Next, let's come together to define a TextView control.
1, create the project Customertextview
As shown in the following:
2. Create Tolistitemview class
This class extends the TextView class, which contains an overridden OnDraw () method, and a constructor method that calls the new Init () method.
The specific code structure is as follows:
Package Com.lyz.customer.textview.activity;import Android.content.context;import Android.graphics.canvas;import Android.util.attributeset;import android.widget.textview;/** * Custom TextView class * Inherit TextView class overrides some methods of TextView * @author Liuyazhuang * */public class Tolistitemview extends TextView {/** * Construction method * @param context * @param attrs * @param defstyle */public Tolistitemview (Context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); init ();} /** * Construction Method * @param context * @param attrs */public Tolistitemview (context context, AttributeSet Attrs) {Super (context, ATT (RS); init ();} /** * Construction Method * @param context */public Tolistitemview (context context) {super (context); init ();} /** * Initialization method */private void init () {}//redraw style @overrideprotected void OnDraw (canvas canvas) {//TODO auto-generated Method St UB}}
3. Create a new Colors.xml file in the Res/values directory
In this file, set a new color value for page, Edge, line, and text
The specific implementation is as follows:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <color name= "Notepad_paper" > #EEF8E0A0 < /color> <color name= "Notepad_lines" > #EE0000FF </color> <color name= "Notepad_margin" > #EE0000FF </color> <color name= "Notepad_text" > #AA0000FF </color></resources>
4. Create a Dimens.xml file
Adds a new value for the width of the page edge.
The specific implementation is as follows:
<resources> <!--Default screen margins, per the Android Design guidelines--><dimen name= "Notepad_ Margin ">30dp</dimen></resources>
5. Custom Tolistitemview Appearance
Create a new private instance variable to store the Paint object that is used to draw the background and edge of the page. Also, create variables to store the color and edge width values of the page, respectively.
By perfecting the Init () method, reference the instance resource created in the first two steps and create a Paint object
The specific implementation code is as follows:
Draw the background of the page private paint marginpaint;//draw the edge of the page private paint linepaint;//Store the color value of the page private int papercolor;// Store the edge width value of the page private float margin;/** * Initialize method */private void init () {//Get references to the most resource tables resources = Getresources ();// Create a brush marginpaint = new Paint (paint.anti_alias_flag) used in the OnDraw method; Marginpaint.setcolor (Resources.getcolor ( R.color.notepad_margin) Linepaint = new Paint (paint.anti_alias_flag); Linepaint.setcolor (Resources.getcolor ( R.color.notepad_lines));//Get page background color and edge width Papercolor = resources.getcolor (r.color.notepad_paper); margin = Resources.getdimension (R.dimen.notepad_margin);}
To start drawing the page, you need to rewrite the OnDraw () method. and use the Paint object you created earlier to draw the image, once the page image is drawn, you can call the parent class's OnDraw () method, and let it draw the text as usual.
The specific implementation code is as follows:
Redraw style @overrideprotected void OnDraw (canvas canvas) {//TODO auto-generated method stub//Paint the color of the page Canvas.drawcolor ( Papercolor);//Draw Edge Canvas.drawline (margin, 0, margin, getmeasuredheight (), linepaint); Canvas.drawline (0, Getmeasuredheight (), Getmeasuredwidth (), Getmeasuredheight (), linepaint);//Draw margincanvas.drawline (margin, 0, Margin, getmeasuredheight (), marginpaint);//move the text so that it crosses the Edge Canvas.save (); canvas.translate (margin, 0);// Use TextView to render text Super.ondraw (canvas); Canvas.restore ();}
The exact complete code is as follows:
Package Com.lyz.customer.textview.activity;import Android.content.context;import android.content.res.Resources; Import Android.graphics.canvas;import Android.graphics.paint;import Android.util.attributeset;import android.widget.textview;/** * Custom TextView class * Some methods of inheriting TextView class overrides TextView * @author Liuyazhuang * */public class Tolistitem View extends TextView {//Draw the page's background private paint marginpaint;//draw the edge of the page private paint linepaint;//Store the color value of the page private int papercolor;//Storage page Edge width value private float margin;/** * Construction method * @param context * @param attrs * @param defstyle */public tolistit Emview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); init ();} /** * Construction Method * @param context * @param attrs */public Tolistitemview (context context, AttributeSet Attrs) {Super (context, ATT (RS); init ();} /** * Construction Method * @param context */public Tolistitemview (context context) {super (context); init ();} /** * Initialize method */private void init () {//Get references to the most resource tables resources resources = getresources ();//create brush margin used in OnDraw methodPaint = new Paint (Paint.anti_alias_flag); Marginpaint.setcolor (Resources.getcolor (R.color.notepad_margin)); Linepaint = new Paint (paint.anti_alias_flag); Linepaint.setcolor (Resources.getcolor (R.color.notepad_lines));// Get the page background color and edge width Papercolor = resources.getcolor (r.color.notepad_paper); margin = Resources.getdimension ( R.dimen.notepad_margin);} Redraw style @overrideprotected void OnDraw (canvas canvas) {//TODO auto-generated method stub//Paint the color of the page Canvas.drawcolor ( Papercolor);//Draw Edge Canvas.drawline (margin, 0, margin, getmeasuredheight (), linepaint); Canvas.drawline (0, Getmeasuredheight (), Getmeasuredwidth (), Getmeasuredheight (), linepaint);//Draw margincanvas.drawline (margin, 0, Margin, getmeasuredheight (), marginpaint);//move the text so that it crosses the Edge Canvas.save (); canvas.translate (margin, 0);// Use TextView to render text Super.ondraw (canvas); Canvas.restore ();}}
6. Create a layout file Todolist_item.xml
This file refers to our custom control class.
The specific implementation is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><com.lyz.customer.textview.activity.tolistitemview xmlns: Android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android: layout_height= "Wrap_content" android:padding= "10DP" android:scrollbars= "vertical" android: Textcolor= "@color/notepad_text" android:fadingedge= "vertical" android:text= "@string/hello_world"/>
7, Perfect Mainactivity class
Set our own definition of view in mainactivity
The specific implementation is as follows:
Package Com.lyz.customer.textview.activity;import Android.app.activity;import Android.os.bundle;import Android.view.menu;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); <span style= "color: #FF0000;" >setcontentview (R.layout.todolist_item); </span>} @Overridepublic Boolean Oncreateoptionsmenu (Menu menu) {/ /Inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
8. androidmanifest.xml File
Finally, we did not do anything in the Androidmanifest.xml file, the contents of the Androidmanifest.xml file are generated automatically, let's give the code in the Androidmanifest.xml file.
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android " package=" com.lyz.customer.textview.activity " android:versioncode=" 1 " android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/>" < Application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string /app_name " android:theme=" @style/apptheme "> <activity android:name=" Com.lyz.customer.textview.activity.MainActivity " android:label=" @string/app_name "> < intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> </ Application></manifest>
9. Operation Effect
Warm tip: You can download the complete custom Control sample code by going to the http://download.csdn.net/detail/l1028386804/8936269 link
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android--Custom TextView