简体   繁体   English

Android:向系统添加自定义字体

[英]Android: add custom fonts to system

I know how to use custom font in an app, but what I want to do is adding custom fonts system-wide, just like adding a new font on Windows.我知道如何在应用程序中使用自定义字体,但我想做的是在系统范围内添加自定义字体,就像在 Windows 上添加新字体一样。 If there's no official way, which module of android source code should I read?如果没有官方方法,我应该阅读android源代码的哪个模块? I have to change android source code and build it to support custom fonts.我必须更改 android 源代码并构建它以支持自定义字体。

Here are steps to add custom font into Android build-in system:以下是将自定义字体添加到 Android 内置系统的步骤:
+ Copy custom font .ttf into frameworks/base/data/fonts + 将自定义字体 .ttf 复制到 frameworks/base/data/fonts
+ Modify framworks/base/data/fonts/Android.mk + 修改framworks/base/data/fonts/Android.mk
Add your custom font into list of 'font_src_files'将您的自定义字体添加到“font_src_files”列表中

font_src_files := \
Roboto-Regular.ttf \
....
AndroidClock_Solid.ttf \
<custom_font>.ttf \ 

+ Modify frameworks/base/data/fonts/fonts.mk + 修改 frameworks/base/data/fonts/fonts.mk
Add your custom font into list of PRODUCT_PACKAGES将您的自定义字体添加到 PRODUCT_PACKAGES 列表中

PRODUCT_PACKAGES := \
DroidSansFallback.ttf \
...
AndroidClock_Solid.ttf \
<custom_font>.ttf \

+ Rebuild + 重建
NOTE: Check if your custom font exists in out/target/product/generic/system/fonts or not.注意:检查您的自定义字体是否存在于 out/target/product/generic/system/fonts 中。 If yes, your custom font have already included in system.如果是,则您的自定义字体已包含在系统中。 If no, recheck your modification.如果不是,请重新检查您的修改。

I'm working on Android 5.1.1 Lollipop.我正在开发 Android 5.1.1 Lollipop。

Nguyen 's answer supported me, but only to some extent. Nguyen的回答支持了我,但只是在某种程度上。 So I have to edit fonts.xml and fallback_fonts.xml files (as commented by Chef Pharaoh ).所以我必须编辑fonts.xmlfallback_fonts.xml文件(如Chef Pharaoh所评论的)。

  • First do the steps Nguyen explained .先做Nguyen解释的步骤。
  • Add following lines in fonts.xml towards the end of the <familyset> element.fonts.xml<familyset>元素的末尾添加以下几行。
<family>
    <font weight="400" style="normal"><custom_font>.ttf</font>
</family>
  • Add following lines in fallback_fonts.xml towards the end of the <familyset> element.fallback_fonts.xml<familyset>元素的末尾添加以下几行。
<family>
    <fileset>
        <file><custom_font>.ttf</file>
    </fileset>
</family>

(replace <custom_font> with your custom font name) (将<custom_font>替换为您的自定义字体名称)

I found this link a bit helpful, and it is in Chinese.我发现这个链接有点帮助,它是中文的。

FYR following note is about the file fallback_fonts.xml FYR 以下注释是关于文件fallback_fonts.xml

NOTE : this file is the legacy format, for compatibility with apps.注意:为了与应用程序兼容,此文件是旧格式。 The new, more flexible format is fonts.xml.新的、更灵活的格式是 fonts.xml。 Please keep the two in sync until the legacy format can be fully removed.请保持两者同步,直到可以完全删除旧格式。

Download Helvetica.ttf file ,copy this file into assets folder and use this code.下载Helvetica.ttf文件,将此文件复制到assets文件夹并使用此代码。

Typeface font = Typeface.createFromAsset(getAssets(), "Helvetica.ttf");

    your_textview_id.setTypeface(font);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM