安卓添加全局字体tff

1.android studio选取Android列表,自动创建assets文件夹
安卓添加全局字体tff_第1张图片
安卓添加全局字体tff_第2张图片

转发:https://www.cnblogs.com/zxxiaoxia/p/7364366.html

2.创建MyApplication

package com.tayh.qrdemo;

import android.app.Application;
import android.graphics.Typeface;

import java.lang.reflect.Field;

public class MyApplication extends Application {
    private  static MyApplication application;

    @Override
    public void onCreate() {
        super.onCreate();
        application=this;
        initType();
    }

    private void initType(){
        Typeface typeface=Typeface.createFromAsset(getAssets(),"fonts/MicrosoftYaHei.ttf");
        Field field = null;
        try {
            field = Typeface.class.getDeclaredField("MONOSPACE");
            field.setAccessible(true);
            field.set(null,typeface);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

3.添加style


    

4.修改manifest,主要添加application和theme


你可能感兴趣的:(Android)