Android修改系统默认字体

文章目录

  • 前言
  • 一、方案
    • 1、将定制的custom_fonts.xml配置文件编译到系统中
    • 2、将自定义的字体ttf文件编译到系统中
    • 3、在系统的编译mk中添加fonts.mk的引用
    • 4、修改系统代码,使得优先加载使用custom_fonts.xml


前言

Android系统中的字体配置文件为/system/etc/fonts.xml
关于fonts.xml文件的解读可以参考
Android fonts相关知识以及fonts.xml解读
Android修改系统默认字体_第1张图片
从图上可以看出,Android系统默认字体名为sans-serif,所使用的是Roboto系列的字体文件。如果不做另外的设置,那么我们写的程序默认会使用的就是sans-serif,也即是Roboto系列的字体。
因此,要修改系统默认的字体,一般的方法也即是修改/system/etc/fonts.xml文件,修改其中的sans-serif为使用其他的ttf文件。
我们在这里不修改源生的fonts.xml文件,而是新增custom_fonts.xml配置文件。主旨是要将字体配置文件以及对应的ttf文件放到vendor下以减小和源码的耦合。

一、方案

方案的主要工作是:

  • 将定制的custom_fonts.xml配置文件编译到系统中。
  • 将自定义的字体ttf文件编译到系统中。
  • 修改系统代码,使得优先加载使用custom_fonts.xml

1、将定制的custom_fonts.xml配置文件编译到系统中

/vendor/XXX/fonts/mydevice_overlay/fonts/Android.mk
这个mk指定了编译custom_fonts.xml模块,会将custom_fonts.xml文件编译到/system/etc/custom_fonts.xml

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := custom_fonts.xml
LOCAL_MODULE_CLASS := ETC
LOCAL_PREBUILT_MODULE_FILE := $(LOCAL_PATH)/custom_fonts.xml

include $(BUILD_PREBUILT)

/vendor/XXX/fonts/mydevice_overlay/fonts/fonts.mk
该mk用于添加custom_fonts.xml模块到系统,并引用了vendor/XXX/fonts/oem-lobster/fonts.mk

PRODUCT_PACKAGES := \
    custom_fonts.xml \

$(call inherit-product-if-exists, vendor/XXX/fonts/oem-lobster/fonts.mk)

/vendor/XXX/fonts/mydevice_overlay/fonts/custom_fonts.xml
custom_fonts.xml是从/system/etc/fonts.xml拷贝而来,我们对其进行一些修改,将调整默认字体从roboto调整为为其他(这里举例为Lobster-Regular.ttf)。如果有别的字体定义内容,也可以一并在此修改。

<familyset version="23">
    
    <family name

你可能感兴趣的:(android)