Android谷歌官方的自适应TextView字体大小的Autosizing。

一、什么是 Autosizeing?

Autosizeing 允许 TextView 根据其内部文本的显示大小,动态的调整其 TextSize 属性值得大小,通过此设置,开发者可以很轻松的在具有动态内容的情况下,对不同的屏幕中,文本大小进行优化。

简单来说,一个 100dp 长度的 TextView ,正常来说只能显示 10 个 10dp 的文字,而如果它的内容超出了 10 个字,以前的通用做法,是通过属性设置,让它在末尾显示 “…” 。而采用了 Autosizeing 这个新特性,它的方案是将字体的尺寸缩小,例如缩小到 8dp,让 TextView 可以容纳下更多的文字,显示完全。而这一切,使用 Autosizeing 我们只需要设置一些属性就可以做到,非常的简单。
在这里插入图片描述
上面这个 Gif 应该可以很直观的描述 Autosizeing 的特性,而它也反映出,触发 Autosizeing 重新计算 TextSize 的时机有两个:

TextView 中的文字增多到无法容纳的地步。
TextView 本身的尺寸被放大或缩小了。

二、相关布局

在这里插入图片描述
1.设置autoSizeTextType属性(开关)

android:autoSizeTextType="uniform"

2、指定最大、最小和梯度值

  android:autoSizeMinTextSize="16sp"
  android:autoSizeMaxTextSize="80sp"
  android:autoSizeStepGranularity="2sp"

3.2、预设一组值,不自动递增
首先在res/values/arrays.xml文件中预设一组值:

 <resources>
  <array name="autosize_text_sizes">
    <item>10sp</item>
    <item>20sp</item>
    <item>30sp</item>
    <item>40sp</item>
    <item>80sp</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" />

注意:使用autoSizeTextType功能的控件,其高度和宽度慎用wrap_content,这会引起一些不可预知的结果。

例子:
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center"
        android:maxLines="1"
        android:text="神圣之光"
        android:textColor="@android:color/white"
        android:textSize="100dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:autoSizeMaxTextSize="100dp"
        android:autoSizeMinTextSize="2dp"
        android:autoSizeTextType="uniform"
        android:background="@android:color/holo_red_light"
        android:gravity="center"
        android:maxLines="1"
        android:text="神圣之光"
        android:textColor="@android:color/white"
        android:textSize="100dp" />
</LinearLayout>

目标是完整显示“神圣之光”字符串,两个TextView都特意把字体大小和TextView的宽度均设为100dp(故意制造无法完整显示完全的情景)。作为对比,蓝色的TextView没有做自适应处理,导致“神圣之光”只能显示一个“神”,而红色的做自适应处理,能自适应的缩小字体完整的显示全部文本“神圣之光”。
注意:要把属性maxLines设置为1,否则可能会换行。
例二
在这里插入图片描述
<?xml version="1.0" encoding="utf-8"?>
<com.zhy.android.percent.support.PercentLinearLayout
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=“wrap_content”
android:orientation=“vertical”>

    <TextView
        android:layout_alignParentTop="true"
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF4081"
        app:layout_heightPercent="5%"
        app:layout_widthPercent="100%"
        android:autoSizeMaxTextSize="80dp"
        android:autoSizeMinTextSize="16dp"
        android:autoSizeTextType="uniform"
        android:text="我的好友"
        android:textSize="26dp"
        android:gravity="center"
        />

</com.zhy.android.percent.support.PercentLinearLayout>
关于百分比布局请见博客:https://blog.csdn.net/qq_21480607/article/details/98479065
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隔壁de小刘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值