自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
添加自定义字体
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您现在可以使用运行时资源叠加层 (RRO) 来更改字体。通过添加自定义字体来制作字体包,您可以在主题背景中使用字体,并使用 RRO 应用自定义设置。
完成以下任务:
custom-fonts.zip 中提供了示例文件结构及示例文件。请下载此文件,然后将其解压缩到本地位置,以供您自己使用。
制作字体包
如需制作字体包,请执行以下操作:
- 将所有字体
ttf
文件添加到 vendor
分区中的字体包内。例如:
vendor/[oem]/fonts/[oem]-sans/
- 为字体包创建
Android.mk
文件。例如:
vendor/[oem]/fonts/[oem]-[fontname]/Android.mk
- 确保模块将安装在
product
分区的 etc
文件夹中。为此,请确认该模块是否具有以下标志:
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
的模块,以供添加到 build 中。此模块指向包含字体系列的 fonts_customization.xml
文件,并会调用所有字体包的 Makefile。例如:
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
下的 build 中,然后重新构建。相应字体将安装到系统中。
- 验证
ttf
字体文件是否位于设备上的 /product/fonts
文件夹中。
- 验证
fonts_customization.xml
是否位于设备上的 /product/etc/
文件夹中。
使用新的系统字体系列
如需使用新的系统字体系列,请使用以下代码:
<style name="customstyle">
<item name="android:fontFamily">customfontfamily</item>
</style>
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):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"]],["最后更新时间 (UTC):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```"]]