简体   繁体   中英

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. If there's no official way, which module of android source code should I read? I have to change android source code and build it to support custom fonts.

Here are steps to add custom font into Android build-in system:
+ Copy custom font .ttf into frameworks/base/data/fonts
+ Modify framworks/base/data/fonts/Android.mk
Add your custom font into list of 'font_src_files'

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

+ Modify frameworks/base/data/fonts/fonts.mk
Add your custom font into list of 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. If yes, your custom font have already included in system. If no, recheck your modification.

I'm working on Android 5.1.1 Lollipop.

Nguyen 's answer supported me, but only to some extent. So I have to edit fonts.xml and fallback_fonts.xml files (as commented by Chef Pharaoh ).

  • First do the steps Nguyen explained .
  • Add following lines in fonts.xml towards the end of the <familyset> element.
<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.
<family>
    <fileset>
        <file><custom_font>.ttf</file>
    </fileset>
</family>

(replace <custom_font> with your custom font name)

I found this link a bit helpful, and it is in Chinese.

FYR following note is about the file fallback_fonts.xml

NOTE : this file is the legacy format, for compatibility with apps. The new, more flexible format is 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.

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

    your_textview_id.setTypeface(font);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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