Re-customizing TextView is a very interesting thing to do, follow ANDROID4 advanced programming, by customizing the TextView, to tap the code:
This is so simple, custom TextView, new Customtextview inherit TextView
public class Customtextview extends TextView {private Paint marginpaint;private paint linepaint;private int PAPERCOLOR;PR ivate Float Margin;public Customtextview (context context, AttributeSet attrs, int defstyle) {Super (context, Attrs, defsty Le); init ();} Public Customtextview (context context, AttributeSet Attrs) {Super (context, attrs); init ();} Public Customtextview (Context context) {super (context); init ();} private void Init () {//Get a reference to the Resource table Resources myresources=getresources ();//Create a brush that will be used in the OnDraw method marginpaint=new paint ( Paint.anti_alias_flag); Marginpaint.setcolor (Myresources.getcolor (R.color.noted_margin)); LinePaint=new Paint ( Paint.anti_alias_flag); Linepaint.setcolor (Myresources.getcolor (R.color.noted_lines));//Get page background color and edge width papercolor= Myresources.getcolor (R.color.noted_paper); margin=myresources.getdimension (R.dimen.noted_margin);} @Overrideprotected void OnDraw (canvas canvas) {//Draw page color Canvas.drawcolor (papercolor);//Draw Edge Canvas.drawline (0, Getmeasuredheight (), Getmeasuredwidth (), Getmeasuredheight(), linepaint);//draw margincanvas.drawline (margin, 0, margin, getmeasuredheight (), marginpaint);//move text, Let it cross the Edge Canvas.save (); canvas.translate (margin, 0);//Use TextView to render text Super.ondraw (canvas); Canvas.restore ();}} XML: <com.example.customtextviewbychen.customtextview android:layout_width= "Match_parent" Android:layo ut_height= "Wrap_content" android:text= "@string/hello_world"/>
It's very important to learn, the knowledge about "OnDraw".
Exercises, customizing TextView (1.1)