1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android auto 字体大小 TextView自动调整字体大小

android auto 字体大小 TextView自动调整字体大小

时间:2021-08-01 09:59:57

相关推荐

android auto 字体大小 TextView自动调整字体大小

系统SDK版本大于等于26,直接使用TextView就可以。

系统SDK版本小于26,需要使用support包,support包的版本要大于等于26.0。support包支持Android 4.0 (API level 14)及以上版本。

2.1 如果Activity继承自AppCompatActivity,直接使用TextView就可以。

2.2 否则需要使用AppCompatTextView。

设置TextView自动调整字体大小

有三种方式可以设置TextView支持自动调整字体大小。

默认设置

控制调整范围

预设大小

注意:如果在XML文件中设置自动调整字体大小,不建议将TextView的宽高设置为wrap_content,不然可能会有意想不到的问题。

默认设置

默认设置允许TextView在水平和垂直轴上均匀调整字体大小。

在代码中使用

根据SDK版本调用不同的方法

//SDK版本大于等于26

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

tvDynamicSet.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM)

} else {

TextViewCompat.setAutoSizeTextTypeWithDefaults(tvDynamicSet,

TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM

)

}

注意:默认情况下缩放的最小字体是12sp,最大的字体是112sp,每次增加或减少的粒度是1px。

在xml文件中使用

如果系统SDK版本大于等于26,使用android命名空间并设置autoSizeTextType属性。该属性取值也有两个none和uniform,对应代码中的两个值。

android:layout_width="match_parent"

android:layout_height="200dp"

android:autoSizeTextType="uniform" />

如果系统SDK版本小于26,因为我们的Activity是继承自AppCompatActivity的,所以我们可以直接使用TextView,但是注意要使用app命名空间。

xmlns:android="/apk/res/android"

xmlns:app="/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="200dp"

app:autoSizeTextType="uniform" />

控制调整范围

你可以定义字体大小调整的一个范围并指定一个渐变值表示每次增加或减少的值。

在代码中使用

//sdk版本大于等于26

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

tvDynamicSet.setAutoSizeTextTypeUniformWithConfiguration(16, 40, 1, PLEX_UNIT_SP

)

} else {

TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(

tvDynamicSet, 16, 40, 1, PLEX_UNIT_SP

)

}

以TextView的setAutoSizeTextTypeUniformWithConfiguration方法为例,3个参数的含义如下:

/**

*

* @param autoSizeMinTextSize 最小字体

* @param autoSizeMaxTextSize 最大字体

* @param autoSizeStepGranularity 渐变值

* @param unit the 尺寸单位 px,sp,dp

*/

在xml中使用

如果系统SDK版本大于等于26,使用android命名空间并设置autoSizeTextType属性为uniform。然后设置autoSizeMinTextSize,autoSizeMaxTextSize和autoSizeStepGranularity这个三个属性。

android:layout_width="match_parent"

android:layout_height="200dp"

android:autoSizeTextType="uniform"

android:autoSizeMinTextSize="12sp"

android:autoSizeMaxTextSize="100sp"

android:autoSizeStepGranularity="2sp" />

如果系统SDK版本小于26,使用app命名空间并设置autoSizeTextType属性为uniform。然后设置autoSizeMinTextSize,autoSizeMaxTextSize和autoSizeStepGranularity这个三个属性。

xmlns:android="/apk/res/android"

xmlns:app="/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="200dp"

app:autoSizeTextType="uniform"

app:autoSizeMinTextSize="12sp"

app:autoSizeMaxTextSize="100sp"

app:autoSizeStepGranularity="2sp" />

预设大小

你可以指定TextView在自动调整字体大小的时候所有可取的值。

在代码中使用

//获取预设的字体大小数字

val intArray = resources.getIntArray(R.array.autosize_text_sizes)

//sdk版本大于等于26

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

tvDynamicSet.setAutoSizeTextTypeUniformWithPresetSizes(intArray, PLEX_UNIT_SP

)

} else {

TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(

tvDynamicSet, intArray, PLEX_UNIT_SP

)

}

在xml中使用

如果系统SDK版本大于等于26,使用android命名空间。设置autoSizeTextType属性和autoSizePresetSizes属性。

android:layout_width="match_parent"

android:layout_height="200dp"

android:autoSizeTextType="uniform"

android:autoSizePresetSizes="@array/autosize_text_sizes" />

在res/values/arrays.xml文件中定义需要的数组。

10sp

12sp

20sp

40sp

100sp

如果系统SDK版本小于26,使用app命名空间并设置autoSizeTextType属性和autoSizePresetSizes属性。

xmlns:android="/apk/res/android"

xmlns:app="/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="200dp"

app:autoSizeTextType="uniform"

app:autoSizePresetSizes="@array/autosize_text_sizes" />

PS:监听用户是否调整了字体大小

可以在Application的重写onConfigurationChanged方法,然后看fontScale是否改变。标准值是1f。大于1f表示用户调大了字体。

@Override

public void onConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);

Logger.d(TAG, "fontScale = " + newConfig.fontScale);

}

参考链接:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。