Android Learning notes-changing the app's font globally

Source: Internet
Author: User

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:

    1. @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;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

Related Article

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: 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.