TextView使用autoSizeTextType自动调整字体大小适应固定宽

本文详细介绍Android中TextView自适应字体大小的三种设置方式:默认、粒度和预设大小,包括XML和代码设置方法,适用于Android8.0及以上版本,同时强调了使用AppCompatTextView的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注意看最下面的注意事项

 

意为:TextView字体大小随着控件的大小变化而变化,Android 8.0(API26)新增,但兼容库 com.android.support:appcompat-v7:26.0.0版本以上已完全兼容到 API14

android_auto_size.gif

 

有三种设置方式:

  • 默认
  • 粒度
  • 预设大小

1.默认

允许TextView在水平轴和垂直轴上均匀地自动调整刻度。

1.1.原生设置

xml设置

<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:autoSizeTextType="uniform" />

代码设置

// 参数: int autoSizeTextType
textView.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM);

1.2.兼容库设置

xml设置

<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:autoSizeTextType="uniform" />

代码设置

// 参数: TextView textView, int autoSizeTextType
TextViewCompat.setAutoSizeTextTypeWithDefaults(textView, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);

参数介绍:
autoSizeTextTypenone: 关闭自动调整功能;uniform: 均匀缩放水平轴和垂直轴

2.粒度

允许TextViewMinTextSize-MaxTextSize之间按StepGranularity的值为增量或减量来跳动。

2.1.原生设置

xml设置

<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"        
    android:autoSizeMaxTextSize="50sp"
    android:autoSizeMinTextSize="30sp"
    android:autoSizeStepGranularity="2sp"
    android:autoSizeTextType="uniform"/>

代码设置

// 参数:int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit
textView.setAutoSizeTextTypeUniformWithConfiguration(30, 50, 2, TypedValue.COMPLEX_UNIT_SP);

2.2.兼容库设置

xml设置

<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:autoSizeMaxTextSize="50sp"
    app:autoSizeMinTextSize="30sp"
    app:autoSizeStepGranularity="2sp"
    app:autoSizeTextType="uniform" />

代码设置

// 参数:TextView textView, int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textView, 30, 50, 2, TypedValue.COMPLEX_UNIT_SP);

参数介绍:
autoSizeMaxTextSize:最大值
autoSizeMinTextSize:最小值
autoSizeStepGranularity:粒度值,即每次增量或减量的值

总结:

1.粒度xml设置时必须设置autoSizeTextType="uniform",代码设置setAutoSizeTextTypeUniformWithConfiguration时则已设置
2.默认设置其实minTextSize = 12sp、maxTextSize = 112sp、granularity = 1px的粒度设置

3.预设大小

允许TextView设置自动调整字体大小跳动时所选的值 。

3.1.原生设置

xml设置

<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"        
    android:autoSizePresetSizes="@array/auto_size_text_sizes"
    android:autoSizeTextType="uniform"/>
<array name="auto_size_text_sizes">
    <item>10sp</item>
    <item>12sp</item>
    <item>20sp</item>
    <item>40sp</item>
    <item>100sp</item>
</array>

代码设置

int[] presetSizes = getResources().getIntArray(R.array.auto_size_text_sizes_code);
// 参数:int[] presetSizes, int unit
textView.setAutoSizeTextTypeUniformWithPresetSizes(presetSizes, TypedValue.COMPLEX_UNIT_SP);
<array name="auto_size_text_sizes_code">
    <item>10</item>
    <item>12</item>
    <item>20</item>
    <item>40</item>
    <item>100</item>
</array>

3.2.兼容库设置

xml设置

<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:autoSizePresetSizes="@array/auto_size_text_sizes"
    app:autoSizeTextType="uniform" />

代码设置

int[] presetSizes = getResources().getIntArray(R.array.auto_size_text_sizes_code);
// 参数:TextView textView, int[] presetSizes, int unit
TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(textView, presetSizes, TypedValue.COMPLEX_UNIT_SP);

参数介绍:
autoSizePresetSizes:预设大小的数组,建议放到res/values/arrays.xml文件里

总结:

1.预设大小xml设置时可以不设置autoSizeTextType="uniform",xml设置autoSizePresetSizes和代码设置setAutoSizeTextTypeUniformWithPresetSizes时都已判断如果autoSizePresetSizes数组的size>0则设置autoSizeTextType="uniform"
2.默认设置其实minTextSize = 12sp、maxTextSize = 112sp、granularity = 1px的粒度设置

注意事项:

1:要设置android:maxLines="n",n可以是任意值,不一定是1,目的就是让其发挥作用,不能用android:singleLine="true";

2:该功能只针对已经存在的TextView的text有效,如果你setText()或append()后改变了text,则改变后不会自适应;

因此在调用setText()或append()后,要重新通过代码调用TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration()方法,例如:

    private void appendText(String text){
        tvCurrentFocus.append(text);
        //自适应大小-只针对已存在的内容生效,改变内容后需要重新设置,因此。。。
        TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(tvCurrentAnswer, 4, 31, 2, TypedValue.COMPLEX_UNIT_SP);
    }

3:一定要使用AppCompatTextView,不要使用TextView,上面例子中虽然用了TextView,请忽略;

4:建议使用兼容包的;

在不同屏幕密度下适配字体大小是移动应用开发中的一个重要问题,因为不同设备的屏幕密度差异很大,直接使用固定的像素值会导致字体在不同设备上显示不一致。为了解决这个问题,可以采用以下几种方法: 1. **使用密度无关像素(dp)**: dp是一种虚拟像素单位,它与设备屏幕的物理像素密度无关。使用dp可以确保在不同密度的屏幕上,字体大小保持一致。 ```xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16dp" android:text="示例文本" /> ``` 2. **使用资源文件进行适配**: 通过在不同的资源文件夹中定义不同的字体大小,可以根据设备的屏幕密度自动选择合适的字体大小。例如: - `res/values/dimens.xml`: ```xml <resources> <dimen name="text_size">16sp</dimen> </resources> ``` - `res/values-sw600dp/dimens.xml`(适用于大屏幕设备): ```xml <resources> <dimen name="text_size">20sp</dimen> </resources> ``` - `res/values-sw720dp/dimens.xml`(适用于超大屏幕设备): ```xml <resources> <dimen name="text_size">24sp</dimen> </resources> ``` 然后在布局文件中引用这些资源: ```xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/text_size" android:text="示例文本" /> ``` 3. **使用可缩放的像素单位(sp)**: sp是另一种虚拟像素单位,它不仅与屏幕密度无关,还与用户的字体大小偏好有关。使用sp可以确保字体大小在不同设备和用户设置下都能自适应。 ```xml <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:text="示例文本" /> ``` 4. **使用Auto-sizing TextView**: Android提供了Auto-sizing TextView功能,可以自动调整文本大小以适应其容器。 ```xml <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:autoSizeTextType="uniform" android:autoSizeMinTextSize="12sp" android:autoSizeMaxTextSize="100sp" android:autoSizeStepGranularity="2sp" android:text="示例文本" /> ``` 通过以上方法,可以在不同屏幕密度下实现字体大小的适配,确保应用在各种设备上都能有良好的显示效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值