对于 Android 8.0(API 级别 26)及更高版本,您可以指示
支持库 26.0 在搭载 Android 8.0(API 级别 26)以下 Android 版本的设备上完全支持 android.support.v4.widget 软件包中包含 TextViewCompat 类,能以向后兼容的方式访问功能。
设置 TextView 自动调整大小
您可以使用框架库或支持库以编程方式或在 XML 中设置 Properties 窗口。
您可以通过以下三种方式设置
注意:如果您在 XML 文件中设置自动调整大小,则不建议对 layout_width 或 layout_height 属性使用值“wrap_content”,否则可能会产生意外结果。
默认
如要以编程方式定义默认设置,请调用 setAutoSizeTextTypeWithDefaults(int autoSizeTextType) 方法。提供 AUTO_SIZE_TEXT_TYPE_NONE 以关闭自动调整大小功能,或提供 AUTO_SIZE_TEXT_TYPE_UNIFORM 以在水平和垂直轴上均匀缩放。
注意:均匀缩放的默认维度为 minTextSize = 12sp、maxTextSize = 112sp 和 granularity = 1px.
如要在 XML 中定义默认设置,请使用 android 命名空间并将 autoSizeTextType 属性设置为 none 或 uniform。<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="200dp"
android:autoSizeTextType="uniform" />
使用支持库
如要通过支持库以编程方式定义默认设置,请调用 TextViewCompat.setAutoSizeTextTypeWithDefaults(TextView textview, int autoSizeTextType) 方法。提供 TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE 或 TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM)的实例。
如要通过支持库在 XML 中定义默认设置,请使用 app 命名空间并将 autoSizeTextType 属性设置为 none 或 uniform。<?xml version="1.0" encoding="utf-8"?>
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">
android:layout_width="match_parent"
android:layout_height="200dp"
app:autoSizeTextType="uniform" />
粒度
您可以定义最小和最大文本大小的范围,以及用于指定每步步长的维度。
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) 方法。提供最大值、最小值、粒度值以及任意
如要通过支持库在 XML 中定义文本大小范围和维度,请使用 app 命名空间并在布局 XML 文件中设置 autoSizeText、autoSizeMinTextSize、autoSizeMaxTextSize 和 autoSizeStepGranularity 属性。<?xml version="1.0" encoding="utf-8"?>
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">
android:layout_width="match_parent"
android:layout_height="200dp"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="12sp"
app:autoSizeMaxTextSize="100sp"
app:autoSizeStepGranularity="2sp" />
预设尺寸
通过预设尺寸,您可以指定
如要使用预设尺寸在 XML 中设置 android 命名空间并设置以下属性:
将 autoSizeText 属性设置为 none 或 uniform。none 是默认值,而 uniform 可让
将 autoSizePresetSizes 属性设置为预设尺寸数组。如要将该数组作为资源来访问,请在 res/values/arrays.xml 文件中定义该数组。
10sp
12sp
20sp
40sp
100sp
android:layout_width="match_parent"
android:layout_height="200dp"
android:autoSizeTextType="uniform"
android:autoSizePresetSizes="@array/autosize_text_sizes" />
使用支持库
如要使用预设尺寸通过支持库以编程方式设置 TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(TextView textView, int[] presetSizes, int unit) 方法。提供
如要使用预设尺寸通过支持库在 XML 中设置 app 命名空间并在布局 XML 文件中设置 autoSizeText 和 autoSizePresetSizes 属性。
10sp
12sp
20sp
40sp
100sp
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">
android:layout_width="match_parent"
android:layout_height="200dp"
app:autoSizeTextType="uniform"
app:autoSizePresetSizes="@array/autosize_text_sizes" />
其他资源
如需详细了解如何在使用动态内容时自动调整 自动调整 TextView 的大小。