Android Replacement fonts

Source: Internet
Author: User

1. You must create a Fonts folder under assets in advance and put in the font file (. ttf) you want to use

2. Write a manager

/** * Set Font * */public class Fontmanager {static String Fongurl = "Fonts/fzltxh.ttf";    Static Typeface TF;        public static void Applyfont (context context, ViewGroup root, String fontname) {Typeface tf = getfont (context);            for (int i = 0; i < Root.getchildcount (); i++) {View v = root.getchildat (i);            if (v instanceof TextView) {((TextView) v). Settypeface (TF);            The else if (v instanceof button) {(button) v). Settypeface (TF);            } else if (v instanceof EditText) {((EditText) v). Settypeface (TF);            } else if (v instanceof viewgroup) {Applyfont (context, (ViewGroup) v, fontname); }}} public static Typeface GetFont (context context) {//if (TF = = null) {//TF = Typeface        . Createfromasset (Context.getassets (), fongurl);//}//return TF;    return typeface.default; }}

  

3. Rewrite TextView

/** * Custom TextView *  */public class Mytextview extends TextView {public    Mytextview (context context) {        super (c Ontext);        Init (context);    }    Public Mytextview (context context, AttributeSet Attrs) {        Super (context, attrs);        Init (context);    }        Public Mytextview (context context, AttributeSet attrs, int defstyle) {        Super (context, attrs, defstyle);        Init (context);    }    private void init (context context) {         Typeface SetFont = fontmanager.getfont (context);         if (SetFont! = null) {             settypeface (SetFont);}}    }

  

4, in the XML directly with Mytextview, stay full path, directly replace TextView

Another type of Textview.settypeface (Fontmanager.getfont ());

Android Replacement fonts

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.