1. 程式人生 > >如何更換Android系統預設字型(Android6.0)

如何更換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)了。