Global Change applied Font, trilogy:
First : Declare a class to inherit application, rewrite the OnCreate () method, remember the declaration in the manifest file (androidmanifest.xml), the code is as follows:
@Overridepublic void OnCreate () {/* * now describes the Setdefaultfont method parameters, the first context object is familiar, the second parameter involves the control will have a property android:typeface *, set the font The Android system supports three fonts by default: "Sans", "serif", "monospace" * As long as you set the Android:typeface= "Monospace" property for the control you want to set the font style to, Once the control has this property detected, the font style of the control is replaced with the "third parameter" font file style; */fontsoverride.setdefaultfont (this, "DEFAULT", "Fonts/simkai.ttf" ); Fontsoverride.setdefaultfont (This, "monospace", "Fonts/simkai.ttf"); Fontsoverride.setdefaultfont (This, "SERIF", "Fonts/simkai.ttf"); Fontsoverride.setdefaultfont (This, "Sans_serif", "Fonts/simkai.ttf"); Super.oncreate ();
second section : Declare a method with the following code:
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 Fontassetname) {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 ();}}}
Part III: theme style must be set, otherwise not effective
<style name= "Apptheme" parent= "Appbasetheme" > <item name= "android:typeface" >monospace</item> </style>
Android Learning notes-changing the app's font globally