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