ปรับขนาด TextView อัตโนมัติ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
สำหรับ Android 8.0 (API ระดับ 26) ขึ้นไป คุณสามารถแนะนำ
TextView
เพื่อให้ข้อความแสดงขนาดได้
ขยายหรือย่อโดยอัตโนมัติเพื่อเติมการจัดวางตาม
ลักษณะและขอบเขตของ TextView
การตั้งค่านี้ช่วยให้คุณ
ปรับขนาดข้อความให้เหมาะสมในหน้าจอต่างๆ ด้วยเนื้อหาแบบไดนามิก
Support Library 26.0 รองรับการปรับขนาด TextView
อัตโนมัติอย่างเต็มรูปแบบ
ในอุปกรณ์ที่ใช้ Android เวอร์ชัน 8.0 (API ระดับ 26) หรือต่ำกว่า
แพ็กเกจ android.support.v4.widget
มี TextViewCompat
ในการเข้าถึงคุณลักษณะในรูปแบบที่เข้ากันได้แบบย้อนหลัง
ตั้งค่าขนาดอัตโนมัติของ TextView
คุณสามารถใช้เฟรมเวิร์กหรือไลบรารีการสนับสนุนเพื่อตั้งค่าการปรับขนาดอัตโนมัติ
TextView
แบบเป็นโปรแกรมหรือใน XML ถึง
ตั้งค่าแอตทริบิวต์ XML คุณยังสามารถใช้คุณสมบัติ
ใน Android Studio
คุณตั้งค่าการปรับขนาด TextView
อัตโนมัติได้ 3 วิธี
ซึ่งอธิบายไว้ในส่วนต่อๆ ไป
หมายเหตุ: หากคุณตั้งค่าการปรับขนาดอัตโนมัติในไฟล์ XML เราจะไม่ดำเนินการต่อไปนี้
ขอแนะนำให้ใช้ค่า "wrap_content" สำหรับ
แอตทริบิวต์ layout_width
หรือ layout_height
ของแอตทริบิวต์
TextView
การดำเนินการนี้อาจ
ผลลัพธ์ที่ไม่คาดคิด
ค่าเริ่มต้น
การตั้งค่าเริ่มต้นจะทำให้การปรับขนาด TextView
เป็นแบบอัตโนมัติ
เหมือนกันบนแกนแนวนอนและแกนแนวตั้ง
- หากต้องการกำหนดการตั้งค่าเริ่มต้นแบบเป็นโปรแกรม ให้เรียกเมธอด
setAutoSizeTextTypeWithDefaults(int autoSizeTextType)
ระบุ AUTO_SIZE_TEXT_TYPE_NONE
เพื่อปิด
ฟีเจอร์การปรับขนาดอัตโนมัติหรือAUTO_SIZE_TEXT_TYPE_UNIFORM
เพื่อ
ปรับสัดส่วนแกนแนวนอนและแกนแนวตั้งให้สม่ำเสมอกัน
หมายเหตุ: ขนาดเริ่มต้นสำหรับแบบเดียวกัน
คือ minTextSize = 12sp
maxTextSize = 112sp
และ granularity = 1px.
- หากต้องการกำหนดการตั้งค่าเริ่มต้นใน XML ให้ใช้เนมสเปซ
android
และตั้งค่า
autoSizeTextType
เป็น none หรือ
แบบเดียวกัน
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:autoSizeTextType="uniform" />
กำหนดการตั้งค่าเริ่มต้นโดยใช้ Support Library
รายละเอียด
คุณสามารถกำหนดช่วงของขนาดข้อความต่ำสุดและสูงสุด
ซึ่งระบุขนาดของแต่ละขั้นตอน
TextView
จะมีสเกลเท่ากันในช่วงระหว่าง
ขนาดต่ำสุดและสูงสุด การเพิ่มแต่ละครั้งที่เกิดขึ้นเป็นขั้นตอน
การตั้งค่าขนาดในแอตทริบิวต์รายละเอียด
กำหนดรายละเอียดโดยใช้ไลบรารีการสนับสนุน
ขนาดที่กำหนดล่วงหน้า
ขนาดที่กำหนดล่วงหน้าให้คุณระบุค่าที่พร็อพเพอร์ตี้
TextView
จะเลือกเมื่อปรับขนาดข้อความอัตโนมัติ
-
เพื่อใช้ขนาดที่กำหนดไว้ล่วงหน้าเพื่อตั้งค่าการปรับขนาดอัตโนมัติ
TextView
แบบเป็นโปรแกรม ให้เรียกฟังก์ชัน
วันที่ setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit)
ระบุอาร์เรย์ขนาดและ TypedValue
สำหรับขนาด
-
เพื่อใช้ขนาดที่กำหนดไว้ล่วงหน้าเพื่อตั้งค่าการปรับขนาดอัตโนมัติ
TextView
ใน XML ให้ใช้ android
Namespace และตั้งค่าแอตทริบิวต์ต่อไปนี้
- ตั้งค่า
autoSizeTextType
เป็น none หรือ uniform คำสั่ง none
เป็นค่าเริ่มต้น และแบบเดียวกันจะ
TextView
ปรับสเกลเท่ากันในแนวนอนและ
แกนแนวตั้ง
- ตั้งค่า
autoSizePresetSizes
เป็นอาร์เรย์ของขนาดที่กำหนดล่วงหน้า ในการเข้าถึงอาร์เรย์เป็น
ให้กำหนดอาร์เรย์ใน res/values/arrays.xml
<resources>
<array name="autosize_text_sizes">
<item>10sp</item>
<item>12sp</item>
<item>20sp</item>
<item>40sp</item>
<item>100sp</item>
</array>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:autoSizeTextType="uniform"
android:autoSizePresetSizes="@array/autosize_text_sizes" />
ตั้งค่าขนาดที่กำหนดล่วงหน้าโดยใช้ไลบรารีการสนับสนุน
- เพื่อใช้ขนาดที่กำหนดไว้ล่วงหน้าเพื่อตั้งค่าการปรับขนาดอัตโนมัติ
TextView
แบบเป็นโปรแกรม
ผ่านทางไลบรารีการสนับสนุน ให้เรียก
TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(TextView textView, int[] presetSizes, int unit)
โปรดระบุอินสแตนซ์ของคลาส TextView
ซึ่งเป็น
อาร์เรย์ของขนาด และหน่วยมิติข้อมูล TypedValue
สำหรับ
ขนาด
- เพื่อใช้ขนาดที่กำหนดไว้ล่วงหน้าเพื่อตั้งค่าการปรับขนาดอัตโนมัติ
TextView
ใน XML ผ่านไลบรารีการสนับสนุน ให้ใช้
เนมสเปซ app
และกำหนด autoSizeTextType
และ
autoSizePresetSizes
ในไฟล์ XML ของเลย์เอาต์
<resources>
<array name="autosize_text_sizes">
<item>10sp</item>
<item>12sp</item>
<item>20sp</item>
<item>40sp</item>
<item>100sp</item>
</array>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
app:autoSizeTextType="uniform"
app:autoSizePresetSizes="@array/autosize_text_sizes" />
</LinearLayout>
แหล่งข้อมูลเพิ่มเติม
ดูข้อมูลเพิ่มเติมเกี่ยวกับการปรับขนาด TextView
อัตโนมัติเมื่อทำงานกับไดนามิก
เนื้อหา ดู
Android Jetpack: การปรับขนาด TextView โดยอัตโนมัติ
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 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-26 UTC"],[],[],null,["# Autosize TextViews\n\nWith Android 8.0 (API level 26) and higher, you can instruct a\n[TextView](/reference/android/widget/TextView) to let the text size\nexpand or contract automatically to fill its layout based on the\n`TextView`'s characteristics and boundaries. This setting makes it easier to\noptimize text size on different screens with dynamic content.\n\n\nSupport Library 26.0 fully supports the autosizing `TextView`\nfeature on devices running Android versions 8.0 (API level 26) or lower.\nThe `android.support.v4.widget` package contains the `TextViewCompat`\nclass to access features in a backward-compatible fashion.\n\nSet up TextView autosize\n------------------------\n\n\nYou can either use the framework or Support Library to set up the autosizing of\n`TextView` programmatically or in XML. To\nset the XML attributes, you can also use the **Properties**\nwindow in Android Studio.\n\n\nThere are three ways you can set up the autosizing of `TextView`,\ndescribed in the sections that follow:\n\n- [Default](#default)\n- [Granularity](#granularity)\n- [Preset sizes](#preset-sizes)\n\n**Note** : If you set autosizing in an XML file, we do not\nrecommended using the value \"wrap_content\" for the\n`layout_width` or `layout_height` attributes of a\n`TextView`. Doing so might produce\nunexpected results.\n\n### Default\n\n\nThe default setting lets the autosizing of `TextView` scale\nuniformly on horizontal and vertical axes.\n\n- To define the default setting programmatically, call the [`setAutoSizeTextTypeWithDefaults(int autoSizeTextType)\n `](/reference/android/widget/TextView#setAutoSizeTextTypeWithDefaults(int)) method. Provide `AUTO_SIZE_TEXT_TYPE_NONE` to turn off the autosizing feature or `AUTO_SIZE_TEXT_TYPE_UNIFORM` to scale the horizontal and the vertical axes uniformly.\n- **Note** : The default dimensions for uniform scaling are `minTextSize = 12sp`, `maxTextSize = 112sp`, and `granularity = 1px.`\n- To define the default setting in XML, use the `android` namespace and set the [`autoSizeTextType`](/reference/android/widget/TextView#attr_android:autoSizeTextType) attribute to *none* or *uniform*. \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cTextView\n android:layout_width=\"match_parent\"\n android:layout_height=\"200dp\"\n android:autoSizeTextType=\"uniform\" /\u003e\n```\n\n#### Define the default setting using the Support Library\n\n- To define the default setting programmatically through the Support Library, call the [`TextViewCompat.setAutoSizeTextTypeWithDefaults(TextView textview, int autoSizeTextType)`](/reference/androidx/core/widget/TextViewCompat#setAutoSizeTextTypeWithDefaults(android.widget.TextView,int)) method. Provide an instance of the `TextView` widget and one of the text types, such as `TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE` or `TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM`.\n- To define the default setting in XML through the Support Library, use the `app` namespace and set the `autoSizeTextType` attribute to *none* or *uniform*. \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cLinearLayout\n xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\u003e\n\n \u003cTextView\n android:layout_width=\"match_parent\"\n android:layout_height=\"200dp\"\n app:autoSizeTextType=\"uniform\" /\u003e\n\n\u003c/LinearLayout\u003e\n```\n\n### Granularity\n\n\nYou can define a range of minimum and maximum text sizes and a\ndimension that specifies the size of each step. The\n`TextView` scales uniformly in a range between the\nminimum and maximum size attributes. Each increment occurs as the step\nsize set in the granularity attribute.\n\n- To define a range of text sizes and a dimension programmatically, call the [setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit)](/reference/android/widget/TextView#setAutoSizeTextTypeUniformWithConfiguration(int, int, int, int)) method. Provide the maximum value, the minimum value, the granularity value, and any [TypedValue](/reference/android/util/TypedValue) dimension unit.\n- To define a range of text sizes and a dimension in XML, use the `android` namespace and set the following attributes:\n - Set the `autoSizeTextType` attribute to either *none* or *uniform* . The *none* value is the default, and *uniform* lets `TextView` scale uniformly on horizontal and vertical axes.\n- Set the [`autoSizeMinTextSize`](/reference/android/widget/TextView#attr_android:autoSizeMinTextSize), [`autoSizeMaxTextSize`](/reference/android/widget/TextView#attr_android:autoSizeMaxTextSize), and [`autoSizeStepGranularity`](/reference/android/widget/TextView#attr_android:autoSizeStepGranularity) attributes to define the dimensions for the autosizing of `TextView`. \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cTextView\n android:layout_width=\"match_parent\"\n android:layout_height=\"200dp\"\n android:autoSizeTextType=\"uniform\"\n android:autoSizeMinTextSize=\"12sp\"\n android:autoSizeMaxTextSize=\"100sp\"\n android:autoSizeStepGranularity=\"2sp\" /\u003e\n```\n\n#### Define granularity using the Support Library\n\n- To define a range of text sizes and a dimension programmatically through the Support Library, call the [`TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit)`](/reference/androidx/core/widget/TextViewCompat#setAutoSizeTextTypeUniformWithConfiguration(android.widget.TextView,int,int,int,int)) method. Provide the maximum value, the minimum value, the granularity value, and any `TypedValue` dimension unit.\n- To define a range of text sizes and a dimension in XML through the Support Library, use the `app` namespace and set the `autoSizeText`, `autoSizeMinTextSize`, `autoSizeMaxTextSize`, and `autoSizeStepGranularity` attributes in the layout XML file. \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cLinearLayout\n xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\u003e\n\n \u003cTextView\n android:layout_width=\"match_parent\"\n android:layout_height=\"200dp\"\n app:autoSizeTextType=\"uniform\"\n app:autoSizeMinTextSize=\"12sp\"\n app:autoSizeMaxTextSize=\"100sp\"\n app:autoSizeStepGranularity=\"2sp\" /\u003e\n\n\u003c/LinearLayout\u003e\n```\n\n### Preset sizes\n\n\nPreset sizes let you specify the values that the\n`TextView` picks when autosizing text.\n\n- To use preset sizes to set up the autosizing of `TextView` programmatically, call the [setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit)](/reference/android/widget/TextView#setAutoSizeTextTypeUniformWithPresetSizes(int[], int)) method. Provide an array of sizes and any `TypedValue` dimension unit for the size.\n- To use preset sizes to set up the autosizing of `TextView` in XML, use the `android` namespace and set the following attributes:\n - Set the `autoSizeTextType` attribute to either *none* or *uniform* . The *none* value is the default, and *uniform* lets `TextView` scale uniformly on horizontal and vertical axes.\n- Set the [`autoSizePresetSizes`](/reference/android/widget/TextView#attr_android:autoSizePresetSizes) attribute to an array of preset sizes. To access the array as a resource, define the array in the `res/values/arrays.xml` file. \n\n```xml\n\u003cresources\u003e\n \u003carray name=\"autosize_text_sizes\"\u003e\n \u003citem\u003e10sp\u003c/item\u003e\n \u003citem\u003e12sp\u003c/item\u003e\n \u003citem\u003e20sp\u003c/item\u003e\n \u003citem\u003e40sp\u003c/item\u003e\n \u003citem\u003e100sp\u003c/item\u003e\n \u003c/array\u003e\n\u003c/resources\u003e\n``` \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cTextView\n android:layout_width=\"match_parent\"\n android:layout_height=\"200dp\"\n android:autoSizeTextType=\"uniform\"\n android:autoSizePresetSizes=\"@array/autosize_text_sizes\" /\u003e\n```\n\n#### Set up preset sizes using the Support Library\n\n- To use preset sizes to set up the autosizing of `TextView` programmatically through the Support Library, call the [`TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(TextView textView, int[] presetSizes, int unit)`](/reference/androidx/core/widget/TextViewCompat#setAutoSizeTextTypeUniformWithPresetSizes(android.widget.TextView,int[],int)) method. Provide an instance of the `TextView` class, an array of sizes, and any `TypedValue` dimension unit for the size.\n- To use preset sizes to set up the autosizing of `TextView` in XML through the Support Library, use the `app` namespace and set the `autoSizeTextType` and `autoSizePresetSizes` attributes in the layout XML file. \n\n```xml\n\u003cresources\u003e\n \u003carray name=\"autosize_text_sizes\"\u003e\n \u003citem\u003e10sp\u003c/item\u003e\n \u003citem\u003e12sp\u003c/item\u003e\n \u003citem\u003e20sp\u003c/item\u003e\n \u003citem\u003e40sp\u003c/item\u003e\n \u003citem\u003e100sp\u003c/item\u003e\n \u003c/array\u003e\n\u003c/resources\u003e\n``` \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cLinearLayout\n xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\u003e\n\n \u003cTextView\n android:layout_width=\"match_parent\"\n android:layout_height=\"200dp\"\n app:autoSizeTextType=\"uniform\"\n app:autoSizePresetSizes=\"@array/autosize_text_sizes\" /\u003e\n\u003c/LinearLayout\u003e\n```\n\nAdditional resources\n--------------------\n\n\nFor additional information on autosizing a `TextView` when working with dynamic\ncontent, watch\n[Android Jetpack: Autosizing TextView](https://www.youtube.com/watch?v=JYrpEAz_A1U)."]]