自动调整 TextView 的大小

对于 Android 8.0(API 级别 26)及更高版本,您可以指示 TextView 允许自动放大或缩小文本大小,以根据 TextView 的特性和边界填充其布局。通过此设置,您可以更轻松地优化包含动态内容的不同屏幕上的文字大小。

支持库 26.0 完全支持在搭载 Android 8.0(API 级别 26)或更低版本的设备上自动调整 TextView 功能。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 属性设置为 noneuniform
  • <?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 命名空间并设置以下属性:
    • autoSizeTextType 属性设置为 noneuniform。默认值为 noneuniform 可允许 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 文件中设置 autoSizeTextTypeautoSizePresetSizes 属性。
  • <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 的大小