新增字型作為 XML 資源
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
試用 Compose
Jetpack Compose 是 Android 推薦的 UI 工具包。瞭解如何在 Compose 中使用文字。
Android 8.0 (API 級別 26) 推出了「XML 中的字型」功能,可讓您將字型做為資源。您可將 font
檔案加入 res/font/
資料夾中,建立字型的套裝組合做為資源。這些字型會編譯在 R
檔案中,並自動出現在 Android Studio 中供您使用。您可以使用 font
資源類型存取字型資源。例如,若要存取字型資源,請使用 @font/myfont
或 R.font.myfont
。
如要在搭載 Android 4.1 (API 級別 16) 以上版本的裝置上使用 XML 中的字型,請使用支援資料庫 26.0。如要進一步瞭解如何使用支援資料庫,請參閱「使用支援資料庫」一節。
如要將字型新增為資源,請在 Android Studio 中執行下列步驟:
- 在「res」資料夾上按一下滑鼠右鍵,然後依序按一下「New」>「Android resource directory」。隨即顯示「新增資源目錄」視窗。
- 在「資源類型」清單中,選取「字型」,然後按一下「確定」。
注意事項:資源目錄的名稱必須是「font」。
圖 1. 新增字型資源目錄。
- 在
font
資料夾中新增字型檔案。 以下資料夾結構會產生 R.font.dancing_script
、R.font.lobster
和 R.font.typo_graphica
。
圖 2. 在 res/font
目錄中新增字型檔案。
- 按兩下字型檔案,即可透過編輯器預覽檔案的字型。
圖 3.
預覽字型檔案。
建立字型系列
字型系列是一組字型檔案,並包含其樣式和粗細詳細資料。
在 Android 中,您可以建立新的字型系列做為 XML 資源,然後以單一單元的形式存取,這樣就不必將每個樣式和粗細視為獨立資源。如此一來,系統就能根據您使用的文字樣式選取正確的字型。
如要建立字型系列,請在 Android Studio 中執行下列步驟:
- 在
font
資料夾上按一下滑鼠右鍵,然後依序選取「New」>「Font resource file」。隨即顯示「New Resource File」視窗。
- 輸入檔案名稱,然後按一下「OK」。新的字型資源 XML 就會在編輯器中開啟。
- 請在
<font>
元素中包含各個字型檔案、樣式和粗細屬性。以下 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>
在 XML 版面配置中使用字型
使用單一字型檔案或字型系列中的字型,在 TextView
物件或樣式中使用 fontFamily
屬性。
注意:使用字型系列時,TextView
會視需要自行切換成使用該系列中的字型檔案。
將字型新增至 TextView
如要設定 TextView
的字型,請執行下列任一操作:
- 在版面配置 XML 檔案中,將
fontFamily
屬性設為要存取的字型檔案。<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/lobster"/>
- 開啟「Properties」視窗,設定
TextView
的字型。
- 選取檢視畫面,開啟「Properties」(屬性) 視窗。
注意事項:只有在設計編輯器開啟時,才能使用「Properties」(屬性) 視窗。選取視窗底部的「Design」分頁標籤。
- 展開「textAppearance」屬性,然後從「fontFamily」清單中選取字型。
-
圖 4.
從「Properties」視窗中選取字型。
如圖 5 的右側窗格中所示,Android Studio 版面配置預覽可讓您預覽在 TextView
中設定的字型。
圖 5.
在版面配置預覽畫面中預覽字型。
在樣式中加入字型
開啟 styles.xml
檔案,然後將 fontFamily
屬性設為要存取的字型檔案。
<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
<item name="android:fontFamily">@font/lobster</item>
</style>
以程式輔助方式使用字型
若要透過程式輔助方式擷取字型,請呼叫 getFont(int)
方法並提供要擷取的字型資源 ID。這個方法會回傳 Typeface
物件。儘管系統會從字型資訊中挑選最適合的樣式,但您也可以使用 setTypeface(android.graphics.Typeface, int)
方法設定特定樣式的字體。
注意:TextView
會為您執行這項作業。
Kotlin
val typeface = resources.getFont(R.font.myfont)
textView.typeface = typeface
Java
Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);
使用支援資料庫
在搭載 Android 4.1 (API 級別 16) 以上版本的裝置上,支援資料庫 26.0 支援 XML 中的字型。
注意:當您透過支援資料庫宣告 XML 版面配置的字型系列時,請使用「app」命名空間確保字型載入。
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont-Regular"/>
<font app:fontStyle="italic" app:fontWeight="400" app:font="@font/myfont-Italic" />
</font-family>
若要透過程式輔助方式擷取字型,請呼叫 ResourceCompat.getFont(Context, int)
方法並提供 Context
的執行個體和資源 ID。
Kotlin
val typeface = ResourcesCompat.getFont(context, R.font.myfont)
Java
Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間:2025-07-26 (世界標準時間)。"],[],[],null,["# Add a font as an XML resource\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to use text in Compose. \n[Set font →](/develop/ui/compose/text/fonts#set-font) \n\n\nAndroid 8.0 (API level 26) introduces fonts in XML, a feature that\nlets you use fonts as resources. You can add the `font` file in\nthe `res/font/` folder to bundle fonts as resources. These fonts\nare compiled in your `R` file and are automatically available in\nAndroid Studio. You can access the font resources using the `font` resource type. For\nexample, to access a font resource,\nuse `@font/myfont`, or `R.font.myfont`.\n\n\nTo use the fonts in XML feature on devices running Android 4.1\n(API level 16) and higher, use Support Library 26.0. For more information\non using the Support Library, refer to the\n[Use the Support Library](#using-support-lib) section.\n\n\nTo add fonts as resources, perform the following steps in Android\nStudio:\n\n\n1. Right-click the **res** folder and go to **New \\\u003e Android resource directory** . The **New Resource Directory** window appears.\n2. In the **Resource type** list, select **font** , then click **OK** .\n\n **Note** : The name of the resource directory must be\n **font**.\n\n\n **Figure 1.** Adding the font resource directory.\n3. Add your font files in the `font` folder.\n\n The folder structure below generates\n `R.font.dancing_script`, `R.font.lobster`, and\n `R.font.typo_graphica`.\n\n\n **Figure 2.** Adding the font files in the `res/font` directory.\n4. Double-click a font file to preview the file's fonts in the editor.\n\n **Figure 3.**\n Previewing the font file.\n\n### Create a font family\n\n\nA font family is a set of font files along with style and weight details.\nIn Android, you can create a new font family as an XML resource and access\nit as a single unit, instead of referencing each style and weight as\nseparate resources. By doing this, you let the system select the correct font\nbased on the text style you are using.\n\nTo create a font family, perform the following steps in Android Studio:\n\n1. Right-click the `font` folder and select **New \\\u003e Font resource file** . The **New Resource File** window appears.\n2. Enter the filename, then click **OK**. The new font resource XML opens in the editor.\n3. Enclose each font file, style, and weight attribute in the `\u003cfont\u003e` element. The following XML illustrates adding font-related attributes in the font resource 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### Use fonts in XML layouts\n\n\nUse your fonts, either a single font file or a font from a\nfont family, in [TextView](/reference/android/widget/TextView)\nobjects or in styles by using the\n`fontFamily` attribute.\n\n**Note:** When you use a font family, the\n`TextView` switches on its own, as needed, to use the\nfont files from that family.\n\n#### Add fonts to a TextView\n\n\nTo set a font for a `TextView`, do one of the\nfollowing:\n\n- In the layout XML file, set the `fontFamily` attribute to the font file you want to access. \n\n ```xml\n \u003cTextView\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:fontFamily=\"@font/lobster\"/\u003e\n ```\n- Open the **Properties** window to set the font for the `TextView`.\n 1. Select a view to open the **Properties** window.\n\n **Note:** The **Properties** window is available\n only when the design editor is open. Select the **Design** tab at\n the bottom of the window.\n 2. Expand the **textAppearance** property, and then select the font from the *fontFamily* list.\n 3.\n\n **Figure 4.**\n Selecting the font from the **Properties** window.\n\n\nThe Android Studio layout preview, shown in the rightmost pane in Figure 5,\nlets you preview the font set in the `TextView`.\n\n**Figure 5.**\nPreviewing fonts in layout preview.\n\n#### Add fonts to a style\n\nOpen the `styles.xml` file and set the `fontFamily`\nattribute to the font file you want to access.\n-\n\n ```xml\n \u003cstyle name=\"customfontstyle\" parent=\"@android:style/TextAppearance.Small\"\u003e\n \u003citem name=\"android:fontFamily\"\u003e@font/lobster\u003c/item\u003e\n \u003c/style\u003e\n ```\n\n### Use fonts programmatically\n\n- To retrieve fonts programmatically, call the [getFont(int)](/reference/android/content/res/Resources#getFont(int)) method and provide the resource identifier of the font you want to retrieve. This method returns a [Typeface](/reference/android/graphics/Typeface) object. Although the system picks the best style for you from the fonts' information, you can use the [setTypeface(android.graphics.Typeface, int)](/reference/android/widget/TextView#setTypeface(android.graphics.Typeface, int)) method to set the typeface with specific styles.\n- **Note:** The `TextView` does this for you. \n\n### Kotlin\n\n```kotlin\nval typeface = resources.getFont(R.font.myfont)\ntextView.typeface = typeface\n```\n\n### Java\n\n```java\nTypeface typeface = getResources().getFont(R.font.myfont);\ntextView.setTypeface(typeface);\n```\n\n### Use the Support Library\n\n- The Support Library 26.0 supports fonts in XML on devices running Android 4.1 (API level 16) and higher.\n- **Note** : When you declare font families in XML layout through the Support Library, use the **app** namespace to ensure that your fonts load. \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cfont-family xmlns:app=\"http://schemas.android.com/apk/res-auto\"\u003e\n \u003cfont app:fontStyle=\"normal\" app:fontWeight=\"400\" app:font=\"@font/myfont-Regular\"/\u003e\n \u003cfont app:fontStyle=\"italic\" app:fontWeight=\"400\" app:font=\"@font/myfont-Italic\" /\u003e\n\u003c/font-family\u003e\n```\n- To retrieve fonts programmatically, call the `ResourceCompat.getFont(Context, int)` method and provide an instance of `Context` and the resource identifier. \n\n### Kotlin\n\n```kotlin\nval typeface = ResourcesCompat.getFont(context, R.font.myfont)\n```\n\n### Java\n\n```java\nTypeface typeface = ResourcesCompat.getFont(context, R.font.myfont);\n```"]]