字体资源
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
字体资源定义了可在应用中使用的自定义字体。字体可以是单独的字体文件,也可以是一组在 XML 中定义的字体文件(称为字体系列)。
另请参阅如何定义 XML 中的字体,或改用可下载字体。
捆绑式字体
您可以将字体作为资源捆绑在应用中。字体在 R
文件中编译,在系统中可自动作为资源使用。然后,您可以借助 font
资源类型访问这些字体。
- 文件位置:
res/font/filename.ttf
(.ttf
、.ttc
、.otf
或 .xml
)
该文件名将用作资源 ID。
- 资源引用:
- 在 XML 中:
@[package:]font/font_name
- 语法:
-
<?xml version="1.0" encoding="utf-8"?>
<font-family>
<font
android:font="@[package:]font/font_to_include"
android:fontStyle=["normal" | "italic"]
android:fontWeight="weight_value" />
</font-family>
- 元素:
-
<font-family>
- 必需。该元素必须是根节点。
无属性。
<font>
- 定义一个字体系列内的单个字体。不包含子节点。
属性:
android:fontStyle
- 关键字。定义字体样式。在将字体加载到字体堆栈中时会使用该属性,并且它会替换字体的标头表格中的任何样式信息。如果您未指定该属性,应用将使用字体标头表格中的值。常量值为
normal
或 italic
。
android:fontWeight
- 整数。字体粗细。在将字体加载到字体堆栈中时会使用该属性,并且它会替换字体的标头表格中的任何粗细信息。属性值必须为 100 到 900(含)之间的 100 的倍数。如果您未指定该属性,应用将使用字体标头表格中的值。最常见的值为 400(常规粗细)和 700(粗体粗细)
- 示例:
- 保存在
res/font/lobster.xml
的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/lobster_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="@font/lobster_italic" />
</font-family>
保存在 res/layout/
中并将该字体应用于 TextView
的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:fontFamily="@font/lobster"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
可下载字体
可下载字体资源定义了可在应用中使用的自定义字体。应用本身不具有这些字体,而是从字体提供程序中检索这些字体。
- 文件位置:
res/font/filename.xml
文件名即为资源 ID。
- 资源引用:
- 在 XML 中:
@[package:]font/font_name
- 语法:
-
<?xml version="1.0" encoding="utf-8"?>
<font-family
android:fontProviderAuthority="authority"
android:fontProviderPackage="package"
android:fontProviderQuery="query"
android:fontProviderCerts="@[package:]array/array_resource" />
- 元素:
-
<font-family>
- 必需。该元素必须是根节点。
属性:
android:fontProviderAuthority
- 字符串。必需。定义字体请求的字体提供程序的授权。
android:fontProviderPackage
- 字符串。必需。要用于请求的字体提供程序的软件包名称。此属性用于验证提供程序的身份。
android:fontProviderQuery
- 字符串。必需。字体的字符串查询。
有关此字符串的格式,请参阅字体提供程序的文档。
android:fontProviderCerts
- 数组资源。必需。定义用于为此提供程序签名的证书的哈希集。此属性用于验证提供程序的身份,且仅当提供程序不属于系统映像的一部分时才需要验证。该值可以指向单个列表(字符串数组资源)或列表的列表(数组资源),其中每个单独的列表代表一个签名哈希集合。有关这些值,请参阅字体提供程序的文档。
- 示例:
- 保存在
res/font/lobster.xml
的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
android:fontProviderAuthority="com.example.fontprovider.authority"
android:fontProviderPackage="com.example.fontprovider"
android:fontProviderQuery="Lobster"
android:fontProviderCerts="@array/certs">
</font-family>
保存在 res/values/
中用于定义证书数组的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="certs">
<item>MIIEqDCCA5CgAwIBAgIJA071MA0GCSqGSIb3DQEBBAUAMIGUMQsww...</item>
</string-array>
</resources>
保存在 res/layout/
中并将该字体应用于 TextView
的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:fontFamily="@font/lobster"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Font resources\n\nA font resource defines a custom font that you can use in your app. Fonts\ncan be individual font files or a collection of font files, known as a\nfont family and defined in XML.\n\nAlso see how to define [fonts\nin XML](/guide/topics/ui/look-and-feel/fonts-in-xml) or instead use [Downloadable Fonts](/guide/topics/ui/look-and-feel/downloadable-fonts).\n\nBundled font\n------------\n\n\nYou can bundle fonts as resources in an app. Fonts are compiled in the\n`R` file and are automatically available in the system as a\nresource. You can then access these fonts with the help of the\n`font` resource type.\n\nfile location:\n: `res/font/`*filename*`.ttf`\n (`.ttf`, `.ttc`, `.otf`, or\n `.xml`) \n\n The filename is used as the resource ID.\n\nresource reference:\n: In XML: `@[package:]font/`*font_name*\n\nsyntax:\n:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cfont-family\u003e\n \u003cfont\n android:font=\"@[package:]font/font_to_include\"\n android:fontStyle=[\"normal\" | \"italic\"]\n android:fontWeight=\"weight_value\" /\u003e\n \u003c/font-family\u003e\n ```\n\nelements:\n:\n\n `\u003cfont-family\u003e`\n : **Required.** This must be the root node.\n\n No attributes.\n\n `\u003cfont\u003e`\n\n : Defines a single font within a family. Contains no child nodes. Attributes:\n\n `android:fontStyle`\n : *Keyword* . Defines the font style. This attribute is\n used when the font is loaded into the font stack and overrides\n any style information in the font's header tables. If you don't\n specify the attribute, the app uses the value from the font's\n header tables. The constant value is either\n `normal` or `italic`.\n\n `android:fontWeight`\n : *Integer*. The weight of the font. This attribute is\n used when the font is loaded into the font stack and overrides\n any weight information in the font's header tables. The\n attribute value must be a multiple\n of 100 between 100 and 900, inclusive. If you don't specify\n the attribute, the app uses the value from the font's header\n tables. The most common values are 400 for regular weight and 700\n for bold weight.\n\nexample:\n : XML file saved at `res/font/lobster.xml`: \n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cfont-family xmlns:android=\"http://schemas.android.com/apk/res/android\"\u003e\n \u003cfont\n android:fontStyle=\"normal\"\n android:fontWeight=\"400\"\n android:font=\"@font/lobster_regular\" /\u003e\n \u003cfont\n android:fontStyle=\"italic\"\n android:fontWeight=\"400\"\n android:font=\"@font/lobster_italic\" /\u003e\n \u003c/font-family\u003e\n ```\n\n XML file saved in `res/layout/` that applies the font to a\n [TextView](/reference/android/widget/TextView):\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cEditText\n android:fontFamily=\"@font/lobster\"\n android:layout_width=\"fill_parent\"\n android:layout_height=\"wrap_content\"\n android:text=\"Hello, World!\" /\u003e\n ```\n\nDownloadable font\n-----------------\n\n\nA downloadable font resource defines a custom font that you can use in an\napp. This font isn't available in the app itself. Instead, the font is\nretrieved from a font provider.\n\nfile location:\n: `res/font/`*filename*`.xml`\n The filename is the resource ID.\n\nresource reference:\n: In XML:`@[package:]font/`*font_name*\n\nsyntax:\n:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cfont-family\n android:fontProviderAuthority=\"authority\"\n android:fontProviderPackage=\"package\"\n android:fontProviderQuery=\"query\"\n android:fontProviderCerts=\"@[package:]array/array_resource\" /\u003e\n ```\n\nelements:\n:\n\n `\u003cfont-family\u003e`\n : **Required.** This must be the root node.\n\n attributes:\n\n `android:fontProviderAuthority`\n : *String* . **Required**. The authority of the font\n provider that defines the font request.\n\n `android:fontProviderPackage`\n : *String* . **Required**. The package name of the font\n provider to be used for the request. This is used to verify the\n identity of the provider.\n\n `android:fontProviderQuery`\n : *String* . **Required**. The string query of the font.\n Refer to your font provider's documentation on the format of this\n string.\n\n `android:fontProviderCerts`\n : *Array resource* . **Required**. Defines the sets of\n hashes for the certificates used to sign this provider. This is\n used to verify the identity of the provider and is only required\n if the provider isn't part of the system image. The value can\n point to a single list (a string array resource) or a list of lists\n (an array resource), where each individual list represents one\n collection of signature hashes. Refer to your font provider's\n documentation for these values.\n\n\nexample:\n : XML file saved at `res/font/lobster.xml`: \n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cfont-family xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:fontProviderAuthority=\"com.example.fontprovider.authority\"\n android:fontProviderPackage=\"com.example.fontprovider\"\n android:fontProviderQuery=\"Lobster\"\n android:fontProviderCerts=\"@array/certs\"\u003e\n \u003c/font-family\u003e\n ```\n\n XML file saved in `res/values/` that defines the cert array:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cresources\u003e\n \u003cstring-array name=\"certs\"\u003e\n \u003citem\u003eMIIEqDCCA5CgAwIBAgIJA071MA0GCSqGSIb3DQEBBAUAMIGUMQsww...\u003c/item\u003e\n \u003c/string-array\u003e\n \u003c/resources\u003e\n ```\n\n XML file saved in `res/layout/` that applies the font to a\n [TextView](/reference/android/widget/TextView):\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cEditText\n android:fontFamily=\"@font/lobster\"\n android:layout_width=\"fill_parent\"\n android:layout_height=\"wrap_content\"\n android:text=\"Hello, World!\" /\u003e\n ```"]]