اندازه خودکار TextViews
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
با Android 8.0 (سطح API 26) و بالاتر، میتوانید به TextView
دستور دهید تا اندازه متن را به طور خودکار بزرگ یا منقبض کند تا طرحبندی آن بر اساس ویژگیها و مرزهای TextView
پر شود. این تنظیم بهینه سازی اندازه متن را در صفحات مختلف با محتوای پویا آسان تر می کند.
کتابخانه پشتیبانی 26.0 به طور کامل از ویژگی اندازهگیری خودکار TextView
در دستگاههای دارای نسخه اندروید 8.0 (سطح API 26) یا پایینتر پشتیبانی میکند. بسته android.support.v4.widget
شامل کلاس TextViewCompat
برای دسترسی به ویژگیها به روشی سازگار با عقب است.
اندازه خودکار TextView را تنظیم کنید
میتوانید از چارچوب یا کتابخانه پشتیبانی برای تنظیم اندازه خودکار TextView
به صورت برنامهنویسی یا XML استفاده کنید. برای تنظیم ویژگی های XML، می توانید از پنجره Properties در 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 یا uniform قرار دهید. <?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:autoSizeTextType="uniform" />
تنظیمات پیش فرض را با استفاده از کتابخانه پشتیبانی تعریف کنید
دانه بندی
شما می توانید محدوده ای از حداقل و حداکثر اندازه متن و ابعادی که اندازه هر مرحله را مشخص می کند، تعریف کنید. TextView
به طور یکنواخت در محدوده ای بین ویژگی های حداقل و حداکثر اندازه مقیاس می شود. هر افزایش به عنوان اندازه مرحله تنظیم شده در ویژگی granularity رخ می دهد.
با استفاده از کتابخانه پشتیبانی، جزئیات را تعریف کنید
اندازه های از پیش تعیین شده
اندازه های از پیش تعیین شده به شما امکان می دهد مقادیری را که TextView
هنگام اندازه گیری خودکار متن انتخاب می کند، مشخص کنید.
- برای استفاده از اندازه های از پیش تعیین شده برای تنظیم اندازه خودکار
TextView
به صورت برنامه ای، روش setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit)
را فراخوانی کنید. آرایه ای از اندازه ها و هر واحد بعدی TypedValue
را برای اندازه ارائه دهید. - برای استفاده از اندازه های از پیش تعیین شده برای تنظیم اندازه خودکار
TextView
در XML، از فضای نام android
استفاده کنید و ویژگی های زیر را تنظیم کنید:- ویژگی
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: Autosizing TextView را تماشا کنید.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-07-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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-29 بهوقت ساعت هماهنگ جهانی."],[],[],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)."]]