Exercises, customizing TextView (1.1)

Source: Internet
Author: User

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)

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.