أحجام النصوص تلقائيًا
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
في إصدار Android 8.0 (المستوى 26 لواجهة برمجة التطبيقات) والإصدارات الأحدث، يمكنك توجيه
TextView
لإتاحة حجم النص
أو التعاقد تلقائيًا لملء تخطيطه بناءً على
سمات TextView
وحدودها يسهل هذا الإعداد
في تحسين حجم النص على الشاشات المختلفة باستخدام المحتوى الديناميكي.
دعم Library Library 26.0 يتيح بشكل كامل ضبط الحجم التلقائي لـ TextView
على الأجهزة التي تعمل بالإصدار 8.0 من نظام التشغيل Android (المستوى 26 من واجهة برمجة التطبيقات) أو الإصدارات الأقدم.
تحتوي حزمة android.support.v4.widget
على TextViewCompat
الوصول إلى الميزات بطريقة متوافقة مع الأنظمة القديمة.
إعداد الحجم التلقائي لـ TextView
ويمكنك استخدام إطار العمل أو مكتبة الدعم لإعداد الحجم التلقائي
TextView
آليًا أو بتنسيق XML. إلى
اضبط سمات XML، يمكنك أيضًا استخدام الخصائص
نافذة جديدة في "استوديو Android"
هناك ثلاث طرق يمكنك من خلالها ضبط حجم 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" />
تحديد الإعداد التلقائي باستخدام Support Library (مكتبة الدعم)
درجة الدقة
يمكنك تحديد نطاق من الحد الأدنى والأقصى لأحجام النص
الذي يحدد حجم كل خطوة. تشير رسالة الأشكال البيانية
يتم قياس TextView
بشكل منتظم في نطاق بين
سمات الحد الأدنى والحد الأقصى للحجم. تحدث كل زيادة كخطوة
الحجم المحدد في تصنيف الدقة.
تحديد الدقة باستخدام "مكتبة الدعم"
أحجام الإعدادات المسبقة
تتيح لك أحجام الإعدادات المسبقة تحديد القيم التي
يختار 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: ضبط حجم TextView تلقائيًا
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-26 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],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)."]]