是否可以为整个应用程序设置自定义字体?

编程入门 行业动态 更新时间:2024-10-26 03:28:55
本文介绍了是否可以为整个应用程序设置自定义字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在整个应用程序中使用某种字体。我有相同的.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>

更多推荐

是否可以为整个应用程序设置自定义字体?

本文发布于:2023-07-23 19:28:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   应用程序   字体

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!