自動調整 TextView 大小
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在 Android 8.0 (API 級別 26) 以上版本中,您可以指示
TextView
可讓文字大小
可根據以下項目自動展開或縮小:
TextView
的特性和界線。方便您
運用動態內容,調整不同螢幕上的文字大小
支援資料庫 26.0 完整支援自動調整 TextView
功能
功能。
android.support.v4.widget
套件包含 TextViewCompat
類別,以回溯相容的方式存取功能。
設定 TextView 自動調整大小
您可以使用架構或支援資料庫,設定自動調整
TextView
。如要設定 XML 屬性,您也可以使用 Android Studio 中的「屬性」視窗。
你可以透過三種方式設定 TextView
的自動調整大小功能。
,
注意:如果您在 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" />
使用支援資料庫定義預設設定
切分單位
您可以定義最小和最大文字大小的範圍,以及指定每個步驟大小的維度。TextView
在最小和最大維度屬性之間的範圍內均勻縮放。每個遞增單位
請在精細程度屬性中設定的尺寸
使用支援資料庫定義精細程度
預設大小
預設大小可讓您指定
TextView
會在自動調整文字大小時選擇。
-
如要使用預設大小,透過程式輔助設定
TextView
的自動調整大小功能,請呼叫 setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit)
方法。提供尺寸陣列和該尺寸的任何 TypedValue
維度單元。
-
如要使用預設大小來設定 XML 中的
TextView
自動調整大小功能,請使用 android
命名空間並設定下列屬性:- 設定
autoSizeTextType
屬性設為 none 或 uniform。無
值為預設值,而 uniform 則可
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
尺寸單位
大小
- 如要使用預設大小來設定自動調整
透過支援資料庫以 XML 形式呈現
TextView
,並使用
app
命名空間,然後將 autoSizeTextType
和
版面配置 XML 檔案中的 autoSizePresetSizes
屬性。
<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 (世界標準時間)。
[null,null,["上次更新時間:2025-07-26 (世界標準時間)。"],[],[],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)."]]