Android switches the third-party font for the entire application, and android third-party font

Source: Internet
Author: User

Android switches the third-party font for the entire application, and android third-party font

I,

1. Obtain and replace the system font through 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 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();        }    }}

2. Modify the font in the Application class (replace the default font of the system)

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");    }}

II,

Modify the configuration file AndroidManifest. xml (only fonts already exist in the system theme)

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">    <item name="android:typeface">monospace</item></style>

3. traverse all text controls in viewGroup 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("setTypeface", 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);

4. Customize controls and set 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"      />



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