在 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將水平軸和垂直軸等距統一縮放。
- 如要在 XML 中定義預設設定,請使用 android命名空間 並設定 將autoSizeTextType屬性設為 none 或 統一。
注意:均勻縮放的預設維度為 minTextSize = 12sp、maxTextSize = 112sp 和 granularity = 1px.
<?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_NONE或TextViewCompat.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 在最小和最大維度屬性之間的範圍內均勻縮放。每個遞增單位
    請在精細程度屬性中設定的尺寸
  
- 如要透過程式輔助來定義文字大小和維度範圍,請呼叫 setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit)方法。請提供最大值、最小值和精細程度 和任何TypedValue維度單位
- 如要以 XML 定義特定文字大小和維度範圍,請使用 android命名空間 以下屬性:- 將
          autoSizeTextType屬性設為 none 或 uniform。none 值 是預設值,而「統一」則可讓您TextView在水平及水平方向上等比例縮放 垂直軸。
- 設定 autoSizeMinTextSize。autoSizeMaxTextSize, 和autoSizeStepGranularity屬性定義自動調整TextView。
 
- 將
          
<?xml version="1.0" encoding="utf-8"?> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:autoSizeTextType="uniform" android:autoSizeMinTextSize="12sp" android:autoSizeMaxTextSize="100sp" android:autoSizeStepGranularity="2sp" />
使用支援資料庫定義精細程度
- 如要透過
      支援資料庫,然後將
      
      TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit)方法。請提供最大值、最小值、精細程度 以及任何TypedValue尺寸單位。
- 如何透過 Support 在 XML 中定義某段文字大小和維度
      程式庫,使用 app命名空間,然後將autoSizeText、autoSizeMinTextSize、autoSizeMaxTextSize和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命名空間並設定下列屬性:- 設定 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。
  
