自動調整 TextView 大小

在 Android 8.0 (API 級別 26) 及以上版本中,您可以指示 TextView 依據 TextView 的特性和邊界自動調整文字大小,以填滿版面配置。該項設定可以讓您在有動態內容的情況下,輕鬆最佳化不同畫面中的文字大小。

支援資料庫 26.0 針對搭載 Android 8.0 (API 級別 26) 之前的 Android 版本裝置,提供自動調整 TextView 功能的完整支援。這個程式庫支援 Android 4.0 (API 級別 14) 及以上版本。android.support.v4.widget 套件包含 TextViewCompat 類別,能以回溯相容性的方式存取功能。

設定 TextView 自動調整大小

您可以使用架構或支援資料庫,透過程式輔助或 XML 設定 TextView 的自動調整大小功能。如要設定 XML 屬性,您也可以使用 Android Studio 中的「屬性」視窗

您可以透過三種方式設定 TextView 的自動調整大小功能:

注意:如果您在 XML 檔案中設定自動調整大小,我們不建議對 TextViewlayout_widthlayout_height 屬性使用「wrap_content」值。這可能會產生非預期的結果。

預設

預設設定讓 TextView 的自動調整大小在水平軸和垂直軸上均勻縮放。

  • 如要透過程式輔助定義預設設定,請呼叫 setAutoSizeTextTypeWithDefaults(int autoSizeTextType) 方法。提供 AUTO_SIZE_TEXT_TYPE_NONE 來關閉自動調整功能,或提供 AUTO_SIZE_TEXT_TYPE_UNIFORM 以均勻縮放水平軸和垂直軸。
  • 注意:均勻縮放的預設維度為 minTextSize = 12spmaxTextSize = 112spgranularity = 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" />
    

使用支援資料庫

  • 如要透過支援資料庫以程式輔助定義預設設定,請呼叫 TextViewCompat.setAutoSizeTextTypeWithDefaults(TextView textview, int autoSizeTextType) 方法。提供 TextView 小工具和其中一個文字類型 (例如 TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONETextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM)。
  • 如要透過支援資料庫在 XML 中定義預設設定,請使用 app 命名空間,並將 autoSizeTextType 屬性設為「none」或「uniform」
  • <?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" />
    
    </LinearLayout>
    

精細程度

您可以定義最小和最大文字大小的範圍,以及指定每個步驟大小的維度。TextView 在最小和最大維度屬性之間的範圍內均勻縮放。累加值會按照精細程度屬性中設定的步距計算。

使用支援資料庫

  • 如要透過支援資料庫以程式輔助方式定義特定文字大小和維度,請呼叫 TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit) 方法。請提供最大值、最小值、精細程度與任何 TypedValue 維度單位。
  • 如要透過支援資料庫在 XML 中定義特定文字大小和維度,請使用 app 命名空間,並設定 autoSizeTextautoSizeMinTextSizeautoSizeMaxTextSize,以及版面配置 XML 檔案中的 autoSizeStepGranularity 屬性。
  • <?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:autoSizeMinTextSize="12sp"
          app:autoSizeMaxTextSize="100sp"
          app:autoSizeStepGranularity="2sp" />
    
    </LinearLayout>
    

預設大小

透過預設大小,您可以指定 TextView 在自動調整文字大小時選擇的所有值。

  • 如要使用預設大小,透過程式輔助設定 TextView 的自動調整大小功能,請呼叫 setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit) 方法。提供尺寸陣列和該尺寸的任何 TypedValue 維度單元。
  • 如要使用預設大小來設定 XML 中的 TextView 自動調整大小功能,請使用 android 命名空間並設定下列屬性:
    • autoSizeText 屬性設為「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 的自動調整大小功能,請呼叫 TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(TextView textView, int[] presetSizes, int unit) 方法。請提供 TextView 類別的執行個體、大小陣列,以及該尺寸的所有 TypedValue 維度單位。
  • 如要使用預設尺寸透過支援資料庫在 XML 中設置 TextView 的自動調整大小功能,請使用 app 命名空間來設定 autoSizeText,以及版面配置 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 大小,請觀看 Autosizing TextView 一文。