ทรัพยากรแบบอักษร
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ทรัพยากรแบบอักษรจะกำหนดแบบอักษรที่กำหนดเองที่คุณใช้ในแอปได้ แบบอักษร
อาจเป็นไฟล์แบบอักษรแต่ละไฟล์หรือคอลเล็กชันไฟล์แบบอักษร ซึ่งเรียกว่า
ชุดแบบอักษรและที่กำหนดไว้ใน XML
นอกจากนี้ โปรดดูวิธีกำหนดแบบอักษร
ใน XML หรือใช้แบบอักษรที่ดาวน์โหลดได้แทน
กลุ่มแบบอักษร
คุณจัดกลุ่มแบบอักษรเป็นทรัพยากรในแอปได้ แบบอักษรจะถูกคอมไพล์ใน
R
และพร้อมใช้งานในระบบโดยอัตโนมัติเป็น
ทรัพยากร จากนั้นคุณสามารถเข้าถึงแบบอักษรเหล่านี้โดยใช้
ประเภททรัพยากร font
- ตำแหน่งที่ตั้งไฟล์:
res/font/filename.ttf
(.ttf
, .ttc
, .otf
หรือ
.xml
)
ชื่อไฟล์จะใช้เป็นรหัสทรัพยากร
- การอ้างอิงทรัพยากร:
- ใน 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 ถึง 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>
ไฟล์ XML ที่บันทึกใน res/layout/
ซึ่งใช้แบบอักษรกับ
TextView
:
<?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
ชื่อไฟล์คือรหัสทรัพยากร
- การอ้างอิงทรัพยากร:
- ใน 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>
ไฟล์ XML ที่บันทึกใน res/values/
ซึ่งกำหนดอาร์เรย์ใบรับรอง:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="certs">
<item>MIIEqDCCA5CgAwIBAgIJA071MA0GCSqGSIb3DQEBBAUAMIGUMQsww...</item>
</string-array>
</resources>
ไฟล์ XML ที่บันทึกใน res/layout/
ซึ่งใช้แบบอักษรกับ
TextView
:
<?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 และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","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-07-27 UTC"],[],[],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 ```"]]