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

╰半橙微兮° 2021-11-09 06:04 727阅读 0赞

一、什么是 Autosizeing?

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

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

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

二、相关布局

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

  1. android:autoSizeTextType="uniform"

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

  1. android:autoSizeMinTextSize="16sp"
  2. android:autoSizeMaxTextSize="80sp"
  3. android:autoSizeStepGranularity="2sp"

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

  1. <resources>
  2. <array name="autosize_text_sizes">
  3. <item>10sp</item>
  4. <item>20sp</item>
  5. <item>30sp</item>
  6. <item>40sp</item>
  7. <item>80sp</item>
  8. </array>
  9. </resources>

布局文件中实现:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView
  3. android:layout_width="match_parent"
  4. android:layout_height="200dp"
  5. android:autoSizeTextType="uniform"
  6. android:autoSizePresetSizes="@array/autosize_text_sizes" />

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

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:layout_width="100dp"
  8. android:layout_height="wrap_content"
  9. android:background="@android:color/holo_blue_bright"
  10. android:gravity="center"
  11. android:maxLines="1"
  12. android:text="神圣之光"
  13. android:textColor="@android:color/white"
  14. android:textSize="100dp" />
  15. <TextView
  16. android:id="@+id/text"
  17. android:layout_width="80dp"
  18. android:layout_height="wrap_content"
  19. android:autoSizeMaxTextSize="100dp"
  20. android:autoSizeMinTextSize="2dp"
  21. android:autoSizeTextType="uniform"
  22. android:background="@android:color/holo_red_light"
  23. android:gravity="center"
  24. android:maxLines="1"
  25. android:text="神圣之光"
  26. android:textColor="@android:color/white"
  27. android:textSize="100dp" />
  28. </LinearLayout>

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

  1. <TextView
  2. android:layout_alignParentTop="true"
  3. android:id="@+id/textView"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:background="#FF4081"
  7. app:layout_heightPercent="5%"
  8. app:layout_widthPercent="100%"
  9. android:autoSizeMaxTextSize="80dp"
  10. android:autoSizeMinTextSize="16dp"
  11. android:autoSizeTextType="uniform"
  12. android:text="我的好友"
  13. android:textSize="26dp"
  14. android:gravity="center"
  15. />
  16. </com.zhy.android.percent.support.PercentLinearLayout>
  17. 关于百分比布局请见博客:https://blog.csdn.net/qq_21480607/article/details/98479065

发表评论

表情:
评论列表 (有 0 条评论,727人围观)

还没有评论,来说两句吧...

相关阅读