One
1. Get system fonts and replace them by reflection
Import Java.lang.reflect.field;import android.content.context;import Android.graphics.typeface;public final class fontsoverride {public static void Setdefaultfont (context context, string statictypefacefieldname, String fo Ntassetname) {final Typeface regular = Typeface.createfromasset (Context.getassets (), Fontassetname) ; Replacefont (statictypefacefieldname, Regular); } protected static void Replacefont (String statictypefacefieldname, final Typeface newtypeface) {try {final Field Staticfield = Typeface.class. Getdeclaredfield (Statictypefacefieldname); Staticfield.setaccessible (TRUE); Staticfield.set (null, newtypeface); } catch (Nosuchfieldexception e) {e.printstacktrace (); } catch (Illegalaccessexception e) {e.printstacktrace (); } }}
2. Perform a modified font in the application class (replace the system default font)
Public final class application extends Android.app.Application { @Override public void OnCreate () { Super.oncreate (); Fontsoverride.setdefaultfont (This, "DEFAULT", "Myfontasset.ttf"); Fontsoverride.setdefaultfont (This, "monospace", "Myfontasset2.ttf"); Fontsoverride.setdefaultfont (This, "SERIF", "Myfontasset3.ttf"); Fontsoverride.setdefaultfont (This, "Sans_serif", "Myfontasset4.ttf");} }
Two
Modified in configuration file Androidmanifest.xml (only fonts already in system theme)
<style name= "Apptheme" parent= "Android:Theme.Holo.Light.DarkActionBar" > <item name= "Android:typeface ">monospace</item></style>
Third, traverse ViewGroup all text controls and set the font
/** * Recursively sets a {@link Typeface} to all * {@link textview}s in a {@link viewgroup}. */public static final void Setappfont (ViewGroup mcontainer, Typeface Mfont, Boolean reflect) {if (Mcontainer = = NULL | | Mfont = = null) return; Final int mCount = Mcontainer.getchildcount (); Loop through all of the children. for (int i = 0; i < MCount; ++i) {final View mchild = Mcontainer.getchildat (i); if (mchild instanceof TextView) {//Set the font if it is a TextView. ((TextView) mchild). Settypeface (Mfont); } else if (Mchild instanceof ViewGroup) {//recursively attempt another viewgroup. Setappfont ((ViewGroup) mchild, Mfont); } else if (reflect) {try {Method msettypeface = Mchild.getclass (). GetMethod ("Set Typeface ", Typeface.class); Msettypeface.invoke (Mchild, Mfont); } catch (Exception e) {/* do something:. */ } } }}
Final Typeface Mfont = Typeface.createfromasset (Getassets (), "Fonts/myfont.ttf"); Final ViewGroup Mcontainer = (viewgroup) Findviewbyid (Android. r.id.content). Getrootview (); Homeactivity.setappfont (Mcontainer, Mfont);
Iv. customizing controls and setting fonts
public class Cusfnttextview extends TextView {public Cusfnttextview (context context, AttributeSet attrs, int defstyle) {
super (context, attrs, defstyle); Init ();} Public Cusfnttextview (context context, AttributeSet Attrs) { Super (context, attrs); Init ();} Public Cusfnttextview (Context context) { super (context); Init ();} private void Init () { if (!isineditmode ()) { Typeface tf = Typeface.createfromasset (GetContext (). Getassets (), " Futura.ttf "); Settypeface (TF);} }
<com.packagename.cusfnttextview android:id= "@+id/tvtitle" android:layout_width= "Wrap_content " android:layout_height= "Wrap_content" android:text= "Hi android" android:textappearance= "? android:attr/ Textappearancelarge " />
Android switches third-party fonts for the entire app