Android新增AppCompatTextView自适应字体大小和文本宽度

Android的supportv7包中新增了一个AppCompatTextView,它是Android标准TextView的增强:

package android.support.v7.widget;

public class AppCompatTextView extends TextView implements TintableBackgroundView,
        AutoSizeableTextView {

}

AppCompatTextView最显著的特点是可以自适应字体宽度大小变化。这个特点很有用,有些开发场景下,UI设计限定了一个文本的宽度,但是文本的长度可能比较长,如果设定一个固定的textSize,就导致一部分文本无法显示。而AppCompatTextView就是为此而生,设定三个参数就可以让文本随着文本宽度的变化,限定在一个固定范围内完整显示出来:

在布局里面把过去的TextView换成AppCompatTextView,然后配置:
android:autoSizeTextType="uniform"
即可自适应宽度。
android:autoSizeMinTextSize="8dp",约束虽然可以自适应调整字体大小,但最小不能小于8dp。
android:autoSizeMaxTextSize="18dp",限定字体大小自适应调整变化时候,最大不能超过的数值。


需要注意的是,根据Android官方对AppCompatTextView的解释说:
/**
 * A {@link TextView} which supports compatible features on older versions of the platform,
 * including:
 *


     *    
  • Allows dynamic tint of its background via the background tint methods in
     *     {@link android.support.v4.view.ViewCompat}.

  •  *    
  • Allows setting of the background tint using {@link R.attr#backgroundTint} and
     *     {@link R.attr#backgroundTintMode}.

  •  *    
  • Supports auto-sizing via {@link android.support.v4.widget.TextViewCompat} by allowing
     *     to instruct a {@link TextView} to let the size of the text expand or contract automatically
     *     to fill its layout based on the TextView's characteristics and boundaries. The
     *     style attributes associated with auto-sizing are {@link R.attr#autoSizeTextType},
     *     {@link R.attr#autoSizeMinTextSize}, {@link R.attr#autoSizeMaxTextSize},
     *     {@link R.attr#autoSizeStepGranularity} and {@link R.attr#autoSizePresetSizes}, all of
     *     which work back to
     *     {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH Ice Cream Sandwich}.

  •  *

 *
 *

This will automatically be used when you use {@link TextView} in your layouts
 * and the top-level activity / dialog is provided by
 * appcompat.
 * You should only need to manually use this class when writing custom views.


 */


这段文档中最后一段比较重要,Android官方提示开发者,如果开发者在xml布局中写了一个过去传统的Android的TextView,那么这个TextView会被自动被Android编译系统替换成AppCompatTextView。在在Android O(8.0)系统及以上,TextView和AppCompatTextView是相同的。在低于8.0的版本上,开发者可在自定义和布局中需要写文本View时候,可以使用AppCompatTextView,以使用到Android最新的自适应大小的特性。

在过去,没有AppCompatTextView官方这一自适应字体大小text view时候,开发者常常使用github上的一个开源项目:

me.grantland.widget.AutofitTextView

然而,AutofitTextView也很久没有维护了。何况现在有了Android官方提供的支持,建议开发者以后可以使用AppCompatTextView用以支持字体的自适应大小变化。

更多相关文章

  1. Android之增强文本框(TextInputEditText)
  2. C#开发微信门户及应用(三)之文本消息和图文消息应答
  3. C#实现添加Word文本与图片超链接的方法
  4. 如何快速生成数据的文本路径呢?C++实现文本路径生成
  5. C++实现逐行读取txt文件中的字符串以及文件文本的复制
  6. c语言中文本输出的函数名称是什么?
  7. golang读取文本乱码解决方法
  8. Go语言使用正则表达式提取网页文本
  9. 不是Web开发者所关注的XML基础具体分析

随机推荐

  1. Android(安卓)文件下载与解压缩
  2. android分享应用工具类
  3. java android 获取手机操作系统相关信息
  4. android 跳转并传递参数
  5. android通知栏响应事件
  6. Android获取GPS进行定位的工具类
  7. android listview 滚动时异步加载图片的
  8. Android之File内部存储和File外部存储
  9. android studio 55[2]线程更新UI handler
  10. 记一次BottomNavigationView使用踩坑