如何更换Android系统默认字体(Android6.0)

Android系统中通过Typeface.java加载字体:

frameworks/base/graphics/java/android/graphics/Typeface.java

private static void init() {
        // Load font config and initialize Minikin state
        File systemFontConfigLocation = getSystemFontConfigLocation();
        File configFilename = new File(systemFontConfigLocation, FONTS_CONFIG);
...
}

该类初始化时File configFilename = new File(systemFontConfigLocation, FONTS_CONFIG);的FONTS_CONFIG为字体的配置文件:

    static Typeface sDefaultTypeface;
    ...

    static final String FONTS_CONFIG = "fonts.xml";

该fonts.xml位于frameworks/base/data/fonts/fonts.xml

<?xml version="1.0" encoding="utf-8"?>
....    
    <family lang="zh-Hans">
        <font weight="400" style="normal">NotoSansSC-Regular.otf</font>
    </family>
    <family lang="zh-Hant">
        <font weight="400" style="normal">NotoSansTC-Regular.otf</font>
    </family>
...

从该xml中发现zh-Hans字段代表中文简体的字体,zh-Hant字段代表中文繁体的字体,目前我们需要将其中的字体NotoSansSC-Regular.otf替换成想要的字体。

实例:将系统默认中文字体修改为宋体常规(simsun.ttc)。

1.把字体simsun.ttc文件copy到frameworks\base\data\fonts目录

2.修改frameworks/base/data/fonts/fonts.xml

    <family lang="zh-Hans">
        <font weight="400" style="normal">simsun.ttc</font>
    </family>

2.修改frameworks\base\data\fonts\Android.mk

################################
# Build the rest of font files as prebuilt.

# $(1): The source file name in LOCAL_PATH.
#       It also serves as the module name and the dest file name.
define build-one-font-module
$(eval include $(CLEAR_VARS))\
$(eval LOCAL_MODULE := $(1))\
$(eval LOCAL_SRC_FILES := $(1))\
$(eval LOCAL_MODULE_CLASS := ETC)\
$(eval LOCAL_MODULE_TAGS := optional)\
$(eval LOCAL_MODULE_PATH := $(TARGET_OUT)/fonts)\
$(eval include $(BUILD_PREBUILT))
endef

font_src_files := \
    Clockopia.ttf \
    AndroidClock.ttf \
    AndroidClock_Highlight.ttf \
    AndroidClock_Solid.ttf \
    simsun.ttc

3.修改frameworks\base\data\fonts\fonts.mk文件

... ...
PRODUCT_PACKAGES := \
    DroidSansFallback.ttf \
    DroidSansMono.ttf \
    Clockopia.ttf \
    AndroidClock.ttf \
    AndroidClock_Highlight.ttf \
    AndroidClock_Solid.ttf \
    simsun.ttc \

4..在frameworks/base/data/fonts目录下单编,执行mm,将生成到对应项目的simsun.ttc文件push到system/fonts下。

5.将修改后的frameworks/base/data/fonts/fonts.xml push 到system/etc目录下,重启设备,系统默认字体就被替换为宋体常规(simsun.ttc)了。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值