字型資源

字型資源定義應用程式中可使用的自訂字型。字型可以是個別的字型檔案;也可以是一組以 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
關鍵字。定義字型樣式。字型載入至字型堆疊時,系統會使用此屬性,並覆寫字型標頭表格中的任何樣式資訊。如未指定屬性,應用程式會使用字型標頭表格中的值。常數值為 normalitalic
android:fontWeight
整數。字型的粗細。字型載入至字型堆疊時,系統會使用此屬性,並覆寫字型標頭表格中的任何粗細資訊。屬性值必須是 100 到 900 (含 100 和 900) 之間的倍數。如未指定屬性,應用程式會使用字型標頭表格中的值。最常見的值為 400 (一般粗細),以及 700 (粗體)。
例如:
XML 檔案儲存在 res/font/lobster.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
「陣列資源」。必要。定義用於簽署此提供者的憑證雜湊組合。此會用於驗證提供者的身分,而且只有在提供者不屬於系統映像檔的一部分時才需要。此值可以指向單一清單 (字串陣列資源) 或清單列表 (陣列資源),其中每個清單都代表一組簽名雜湊。請參閱這些值的字型提供者說明文件。
例如:
XML 檔案儲存在 res/font/lobster.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>

將字型套用在 TextView 的 XML 檔案儲存在 res/layout/ 中:

<?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!" />