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

AppCompatTextView它是Android标准TextView的增强:

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

注意:是 app:autoSizeTextType=“uniform” 而不是 android:autoSizeTextType=“uniform”

    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        app:autoSizeMaxTextSize="18dp"
        app:autoSizeMinTextSize="8dp"
        app:autoSizeTextType="uniform"
        android:maxLines="1"
        android:text="林伟茂林伟茂"
        android:textSize="18dp" />

在布局里面把过去的TextView换成AppCompatTextView,然后配置:

  • app:autoSizeTextType=“uniform” ,即可自适应宽度。
  • app:autoSizeMinTextSize=“8dp”,约束虽然可以自适应调整字体大小,但最小不能小于8dp。
  • app: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用以支持字体的自适应大小变化。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值