Android 8.0 新特性(四) - 自动调整TextView


Autosizing TextViews
Demo地址:AutosizingTextViewDemo

在Android O中,TextView可用自动放大或缩小文本的大小,以便根据TextView的属性和边界填充其布局。这样设置可以更轻松地在不同屏幕上优化动态内容的文本大小。

Support 26支持在运行Android 8.0(API 26)之前版本的设备上自动调整TextView功能,最低API要求是 API 14。在android.support.v4.widget中的TextViewCompat类,以兼容此特性。

设置TextView自动调整大小

可以使用框架或支持库以编程方式或以XML格式设置TextView的自动调整大小。要设置XML属性,还可以使用Android Studio中的“Attribute”窗口。

有三种方式可以设置TextView的自动调整大小:

  1. 默认(Default)
  2. 间隔尺寸(Granularity)
  3. 预设尺寸(Preset Sizes)

注意:如果在XML文件中设置自动调整,则不建议对TextView的layout_width或layout_height属性使用值“wrap_content”。

默认(Default)

默认设置使TextView的自动调整尺寸在水平轴和垂直轴上均匀。

  • 要以编程方式定义默认设置,调用setAutoSizeTextTypeWithDefaults(int autoSizeTextType)方法。

    • AUTO_SIZE_TEXT_TYPE_NONE:关闭自动调整功能
    • AUTO_SIZE_TEXT_TYPE_UNIFORM:以均衡缩放水平轴和垂直轴

    统一缩放的默认维度为minTextSize = 12sp,maxTextSize = 112sp,间隔尺寸= 1px

  • 要在XML中定义默认设置,使用android命名空间,将autoSizeTextType属性设置为none或uniform


<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:autoSizeTextType="uniform" />

使用支持库

  • 要通过支持库以编程方式定义默认设置,调用TextViewCompat.setAutoSizeTextTypeWithDefaults(TextView textview,int autoSizeTextType)方法。提供TextView控件的实例和文本类型。文本类型如下:

    • TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE:关闭自动调整功能
    • TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM:以均衡缩放水平轴和垂直轴
  • 要通过支持库定义XML中的默认设置,使用app命名空间,将autoSizeTextType属性设置为none或uniform


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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="match_parent">

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

</LinearLayout>

间隔尺寸(Granularity)

可以定义最小和最大文本尺寸的范围以及指定每个步长的尺寸。TextView在最小和最大文本尺寸之间的范围内均匀地缩放。每个增量按照autoSizeStepGranularity属性中设置的步长进行缩放。

  • 要以编程方式定义文本大小和尺寸范围,调用setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize,int autoSizeMaxTextSize,int autoSizeStepGranularity,int unit)方法。

    • autoSizeMinTextSize:最小值
    • autoSizeMaxTextSize:最大值
    • autoSizeStepGranularity:步长
    • unit:任何TypedValue尺寸单位
  • 要在XML中定义一系列文本大小和尺寸,使用android命名空间并设置以下属性:

    • android:autoSizeTextType:none或uniform
    • android:autoSizeMinTextSize
    • android:autoSizeMaxTextSize
    • android:autoSizeStepGranularity


<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:autoSizeTextType="uniform"
    android:autoSizeMinTextSize="12sp"
    android:autoSizeMaxTextSize="100sp"
    android:autoSizeStepGranularity="2sp" />

使用支持库

  • 要以编程方式定义文本大小和尺寸范围,调用TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit) ,提供以下参数:

    • autoSizeMinTextSize:最小值
    • autoSizeMaxTextSize:最大值
    • autoSizeStepGranularity:步长
    • unit:任何TypedValue尺寸单位
  • 要在XML中定义一系列文本大小和尺寸,使用app命名空间并设置以下属性:

    • app:autoSizeTextType:none或uniform
    • app:autoSizeMinTextSize
    • app:autoSizeMaxTextSize
    • app:autoSizeStepGranularity


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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="match_parent">

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

</LinearLayout>

预设尺寸(Preset Sizes)

预设尺寸可以让在自动自动调整文本时TextView选择的所有指定的值。

  • 以编程方式设置TextView的自动调整,调用setAutoSizeTextTypeUniformWithPresetSizes(int [] presetSizes,int unit)方法。

    • presetSizes:指定的文本尺寸列表
    • unit:任何TypedValue尺寸单位
  • 在设置XML中设置TextView自动调整大小,使用android命名空间并设置以下属性。在res/values/ arrays.xml文件中定义尺寸数组,将尺寸数组作为资源访问:

    • android:autoSizeTextType:none或uniform
    • android:autoSizePresetSizes


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

使用支持库

  • 以编程方式设置TextView的自动调整,TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(TextView textView, int[] presetSizes, int unit),提供以下参数

    • textView
    • presetSizes::指定的文本尺寸列表
    • unit:任何TypedValue尺寸单位
  • 在设置XML中设置TextView自动调整大小,使用app命名空间并设置以下属性。在res/values/ arrays.xml文件中定义尺寸数组,将尺寸数组作为资源访问:

    • app:autoSizeTextType:none或uniform
    • app:autoSizePresetSizes


<resources>
  <array name="autosize_text_sizes">
    <item>10sp</item>
    <item>12sp</item>
    <item>20sp</item>
    <item>40sp</item>
    <item>100sp</item>
  </array>
</resources>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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="match_parent">

  <TextView
      android:layout_width="match_parent"
      android:layout_height="200dp"
      app:autoSizeTextType="uniform"
      app:autoSizePresetSizes="@array/autosize_text_sizes" />
</LinearLayout>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值