App导入第三方字体库

1.下载字体库,如思源黑体SourceHanSansCN-Bold.otf
2.app->src->main 新建assets->font->SourceHanSansCN-Bold.otf

单个textview引用

        TextView tv = findViewById(R.id.test);
        Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/SourceHanSansCN-Bold.otf");
        tv.setTypeface(typeface);

全局引用字体复杂一些,需要新建application 并设置style ,在mainfest里使用

public class AppApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Typeface mTypeface = Typeface.createFromAsset(getAssets(), "fonts/SourceHanSansCN-Bold.otf");
        Field field = null;
        try {
            field = Typeface.class.getDeclaredField("MONOSPACE");
            field.setAccessible(true);
            field.set(null,mTypeface);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

res->values->styles.xml

    <style name="theme_fullscreen" parent="AppTheme">
        <item name="android:typeface"> monospaceitem>
    style>

mainfest

".AppApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/theme_fullscreen">

你可能感兴趣的:(Android)