自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
新增自訂字型
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您現在可以使用執行階段資源覆蓋 (RRO) 變更字型。新增自訂字型來製作字型套件,即可在主題中使用字型,並使用 RRO 套用自訂項目。
完成下列工作:
custom-fonts.zip 提供檔案結構和範例檔案。請下載並在本機解壓縮這個檔案,以供您使用。
製作字型套件
如何製作字型套件:
- 將所有字型
ttf
檔案新增至 vendor
分割區中的字型套件。例如:
vendor/[oem]/fonts/[oem]-sans/
- 為字型套件建立
Android.mk
檔案。例如:
vendor/[oem]/fonts/[oem]-[fontname]/Android.mk
- 請確認模組會安裝在
etc
資料夾的 product
分區中。如要這樣做,請確認模組具有下列標記:LOCAL_MODULE_CLASS := ETC
LOCAL_PRODUCT_MODULE := true
LOCAL_MODULE_PATH := $(TARGET_OUT_PRODUCT)/fonts
上述程式碼範例說明瞭字型套件模組的完整定義。
- 為字型套件建立名為
fonts.mk
的檔案,然後將字型 ttf
檔案新增至 PRODUCT_PACKAGES
。例如:
vendor/[oem]/fonts/[oem]-[fontname]/fonts.mk
例如:
PRODUCT_PACKAGES := \
[font name].ttf \
建立裝置重疊套件
- 建立名為
fonts_customization.xml
的模組,以便新增至建構。這個模組會指向包含字型系列的 fonts_customization.xml
檔案,並呼叫所有字型套件的製作檔案。例如:
vendor/[oem]/[device]_overlay/fonts/Android.mk
- 建立字型系列:
vendor/[oem]/[device]_overlay/fonts/fonts_customization.xml
檔案的根層級必須是 fonts-modification
標記。為這個元素下方的每個字型套件新增字型系列。此外,請將 customizationType="new-named-family"
新增至每個字型系列,並確保每個字型系列都有 name
屬性。這個名稱稍後會用於存取字型系列。這個檔案必須新增至先前在 Android.mk
中定義的 fonts_customizations.xml
模組。
上述範例包含有效的 fonts_customization.xml
檔案。
- 建立
fonts.mk
。例如:
vendor/[oem]/[device]_overlay/fonts/fonts.mk
- 在
PRODUCT_PACKAGES
標記下方新增 fonts_customization.xml
。
- 呼叫先前建立的每個字型套件。
$(call inherit-product-if-exists, vendor/[oem]/fonts/[oem]-[fontname]/fonts.mk)
- 將字型模組新增至
PRODUCT_PACKAGES
下的建構項目,然後重新建構。系統會安裝字型。
- 確認
ttf
字型檔案位於裝置上的 /product/fonts
資料夾中。
- 確認
fonts_customization.xml
在裝置上屬於 /product/etc/
。
使用新的系統字型系列
如要使用新的系統字型系列,請按照下列步驟操作:
<style name="customstyle">
<item name="android:fontFamily">customfontfamily</item>
</style>
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-10 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-10 (世界標準時間)。"],[],[],null,["# Add custom fonts\n\nYou can now use runtime resource overlays (RROs) to change fonts. By adding custom\nfonts to make a font package, you can use fonts in themes and use RROs to apply the\ncustomizations.\n\nComplete these tasks:\n\n- [Make a font package](/docs/automotive/hmi/car_ui/fonts#make)\n- [Create a device overlay package](/docs/automotive/hmi/car_ui/fonts#create)\n- [Use the added system fonts family](/docs/automotive/hmi/car_ui/fonts#use)\n\nA sample file structure and sample files are provided in\n[custom-fonts.zip](/static/docs/automotive/hmi/car_ui/custom-fonts.zip).\nDownload and then extract this file locally for your own use.\n\nMake a font package\n-------------------\n\nTo make a font package:\n\n1. Add all the font `ttf` files to the font package in the `vendor` partition. For example:\n\n `vendor/[oem]/fonts/[oem]-sans/`\n2. Create the `Android.mk` file for the font package. For example:\n\n `vendor/[oem]/fonts/[oem]-[fontname]/Android.mk`\n3. Make sure the module will be installed in the `product` partition in the `etc` folder. To do so, confirm the module has the following flags: \n\n ```makefile\n LOCAL_MODULE_CLASS := ETC\n LOCAL_PRODUCT_MODULE := true\n LOCAL_MODULE_PATH := $(TARGET_OUT_PRODUCT)/fonts\n ```\n\n The sample code provided above illustrates a complete definition of a font package module.\n4. Create a file named `fonts.mk` for the font package and then add the font `ttf` files to `PRODUCT_PACKAGES`. For example:\n\n `vendor/[oem]/fonts/[oem]-[fontname]/fonts.mk`\n\n For example: \n\n ```objective-c\n PRODUCT_PACKAGES := \\\n [font name].ttf \\\n ```\n\nCreate a device overlay package\n-------------------------------\n\n1. Make a module called `fonts_customization.xml` to be added to the build. This module points to the `fonts_customization.xml` file that contains the font families and calls the make files of all the font packages. For example:\n\n `vendor/[oem]/[device]_overlay/fonts/Android.mk`\n2. Create the font families: `vendor/[oem]/[device]_overlay/fonts/fonts_customization.xml`\n\n The root level of the file must be a `fonts-modification` tag. Add\n font families for each of the font packages under this element. In addition, add\n `customizationType=\"new-named-family\"` to every font family, and make\n sure each has a `name` attribute. This name is used later to access the\n font family. This file must be added to the `fonts_customizations.xml`\n module defined previously in `Android.mk`.\n\n The sample provided above contains a valid `fonts_customization.xml` file.\n3. Create `fonts.mk`. For example:\n\n `vendor/[oem]/[device]_overlay/fonts/fonts.mk`\n4. Add `fonts_customization.xml` under the `PRODUCT_PACKAGES` flag.\n5. Call into each of the previously created font packages.\n `$(call inherit-product-if-exists, vendor/[oem]/fonts/[oem]-[fontname]/fonts.mk)`\n\n6. Add the font module to the build under `PRODUCT_PACKAGES` and then rebuild. The fonts will be installed on the system.\n7. Verify that the `ttf` fonts files are in the `/product/fonts` folder on the device.\n8. Verify that `fonts_customization.xml` is in `/product/etc/` on the device.\n\nUse the new system fonts family\n-------------------------------\n\nTo use the new system fonts family: \n\n```text\n\u003cstyle name=\"customstyle\"\u003e\n \u003citem name=\"android:fontFamily\"\u003ecustomfontfamily\u003c/item\u003e\n\u003c/style\u003e\n```"]]