使用Android 8.0(API级别26)及更高版本,您可以指示a TextView让文本大小自动扩展或收缩,以根据其TextView特征和边界填充其布局 。此设置可以更轻松地使用动态内容优化不同屏幕上的文本大小。
支持库26.0完全支持 TextView在Android 8.0(API级别26)之前运行Android版本的设备上的自动调整功能。该库提供对Android 4.0(API级别14)及更高版本的支持。该android.support.v4.widget 软件包包含TextViewCompat以向后兼容方式访问功能的类
设置TextView自动调整大小
您可以使用框架或支持库以 TextView编程方式或以XML格式设置自动调整大小。要设置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均匀缩放水平轴和垂直轴。
注:为统一缩放的默认尺寸minTextSize = 12sp, maxTextSize = 112sp以及 granularity = 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_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命名空间并设置以下属性:
将 autoSizeText 属性设置为none或uniform。none 是默认值,uniform可以 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维度单位。
要定义范围的文本大小,并通过支持库中的XML格式的尺寸,使用的app命名空间和设置 autoSizeText,autoSizeMinTextSize, autoSizeMaxTextSize,和 autoSizeStepGranularity布局XML文件中的属性。
<?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 尺寸单位的数组。
要使用预设大小设置 TextViewXML的自动调整大小,请使用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维度单位。
要使用预设大小TextView通过支持库设置XML的自动调整大小 ,请使用布局XML文件中的 app命名空间和set autoSizeText和 autoSizePresetSizesattributes。
<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。
联系我
QQ:94297366
微信打赏:https://pan.baidu.com/s/1dSBXk3eFZu3mAMkw3xu9KQ
公众号推荐: