是否可以为整个应用程序设置自定义字体? [英] Is it possible to set a custom font for entire of application?

查看:157
本文介绍了是否可以为整个应用程序设置自定义字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在整个应用程序中使用某种字体。我有相同的.ttf文件。
是否可以在应用程序启动时将其设置为默认字体,然后在应用程序的其他位置使用它?设置后,如何在布局XML中使用它?

I need to use certain font for my entire application. I have .ttf file for the same. Is it possible to set this as default font, at application start up and then use it elsewhere in the application? When set, how do I use it in my layout XMLs?

推荐答案

是的,带有反射。这有效(基于此答案):

Yes with reflection. This works (based on this answer):

(注意:由于缺乏对自定义字体的支持,这是一种解决方法,因此如果您想要更改这种情况,请点击以对 android issue here )。 注意:不要在这个问题上留下我也是评论,每个看过它的人都会收到一封电子邮件。所以请它明星吧。

(Note: this is a workaround due to lack of support for custom fonts, so if you want to change this situation please do star to up-vote the android issue here). Note: Do not leave "me too" comments on that issue, everyone who has stared it gets an email when you do that. So just "star" it please.

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

然后你需要重载几个默认字体,例如在应用程序类中:

You then need to overload the few default fonts, for example in an application class:

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

当然,如果你使用相同的字体文件,你可以改进它只加载一次。

Or course if you are using the same font file, you can improve on this to load it just once.

但是我倾向于只重写一个,比如说MONOSPACE,然后设置一个样式以强制该字体字体应用程序:

However I tend to just override one, say "MONOSPACE", then set up a style to force that font typeface application wide:

<resources>
    <style name="AppBaseTheme" parent="android:Theme.Light">
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:typeface">monospace</item>
    </style>
</resources>



API 21 Android 5.0



我' ve调查了评论中的报告它不起作用,它似乎与主题 android:Theme.Material.Light 不相容。

如果该主题对您不重要,请使用较旧的主题,例如:

If that theme is not important to you, use an older theme, e.g.:

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

这篇关于是否可以为整个应用程序设置自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆