自动调整 TextView 大小

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

Support Library 26.0 完全支持在 Android 8.0(API 级别 26)或更低版本设备上自动调整 TextView 大小的功能。android.support.v4.widget 软件包包含 TextViewCompat 类,用于以向后兼容的方式访问功能。

设置 TextView 自动调整大小

您可以使用框架或 Support Library 通过编程方式或在 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 属性设置为 noneuniform
  • <?xml version="1.0" encoding="utf-8"?>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:autoSizeTextType="uniform" />

使用 Support Library 定义默认设置

  • 要通过 Support Library 以编程方式定义默认设置,请调用 TextViewCompat.setAutoSizeTextTypeWithDefaults(TextView textview, int autoSizeTextType) 方法。提供 TextView 微件的实例和其中一种文本类型,例如 TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONETextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM
  • 要通过 Support Library 在 XML 中定义默认设置,请使用 app 命名空间,并将 autoSizeTextType 属性设置为 noneuniform
  • <?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 在最小和最大尺寸属性之间的范围内均匀缩放。每次增量的大小与粒度属性中设置的步长大小一致。

使用 Support Library 定义粒度

  • 要通过 Support Library 以编程方式定义文本大小范围和尺寸,请调用 TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit) 方法。提供最大值、最小值、粒度值以及任何 TypedValue 尺寸单位。
  • 要通过 Support Library 在 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 属性设置为 noneuniformnone 是默认值,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" />

使用 Support Library 设置预设大小

  • 要通过 Support Library 以编程方式使用预设大小设置 TextView 自动调整大小功能,请调用 TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(TextView textView, int[] presetSizes, int unit) 方法。提供 TextView 类的实例、一个大小数组以及大小的任何 TypedValue 尺寸单位。
  • 要通过 Support Library 在 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 大小