自动调整 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 中的 Properties 窗口。

您可以通过以下三种方式设置 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 命名空间并在布局 XML 文件中设置 autoSizeTextautoSizeMinTextSizeautoSizeMaxTextSizeautoSizeStepGranularity 属性。
  • <?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 命名空间并在布局 XML 文件中设置 autoSizeTextautoSizePresetSizes 属性。
  • <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 的大小,请观看自动调整 TextView 的大小