TextView の自動サイズ調整
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Android 8.0(API レベル 26)以降では、
TextView
: テキストサイズを調整します
サイズに応じて、レイアウトに合わせて自動的に拡大または縮小
TextView
の特性と境界。この設定により、
動的コンテンツを使用して、さまざまな画面でテキストサイズを最適化します。
サポート ライブラリ 26.0 が TextView
の自動サイズ設定を完全にサポート
この機能は、Android バージョン 8.0(API レベル 26)以前を搭載しているデバイスで利用できます。
android.support.v4.widget
パッケージには、TextViewCompat
が含まれています。
下位互換性のある方法で機能にアクセスできます。
TextView の自動サイズ調整を設定する
フレームワークまたはサポート ライブラリを使用して、
TextView
。プログラムまたは XML で記述します。XML 属性を設定するには、Android Studio の [Properties] ウィンドウも利用できます。
TextView
の自動サイズ調整を設定する方法は 3 つあります。
以降のセクションで説明します。
注: XML ファイルで自動サイズ設定を設定した場合、
値「wrap_content」を使用することをおすすめします。の
属性の layout_width
属性または layout_height
属性
TextView
。その結果、
想定外の結果になることもあります
デフォルト
デフォルトの設定では、TextView
の自動サイズ調整が有効になります。
水平軸と垂直軸で一様に表示します。
- デフォルトの設定をプログラムで定義するには、
<ph type="x-smartling-placeholder"></ph>
setAutoSizeTextTypeWithDefaults(int autoSizeTextType)
メソッドを使用します。無効にするには AUTO_SIZE_TEXT_TYPE_NONE
を指定してください
自動サイズ調整機能、または AUTO_SIZE_TEXT_TYPE_UNIFORM
水平軸と垂直軸を均等にスケーリングします。
注: 均一な拡大縮小のデフォルトのディメンションは、minTextSize = 12sp
、maxTextSize = 112sp
、granularity = 1px.
です。
- XML でデフォルト設定を定義するには、
android
名前空間を使用します。
を設定します。
autoSizeTextType
属性を none または
uniform:
<?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 のいずれかに指定します。none
デフォルト値で、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
サポート ライブラリを使用する場合は、
<ph type="x-smartling-placeholder"></ph>
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 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)."]]