Android switches third-party fonts for the entire app

Source: Internet
Author: User

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

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.