android - 如何以编程方式将 autoSizeTextType 统一?

标签 android textview

这是可以做到的

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autoSizeTextType="uniform"
    android:maxLines="1" />

但是属性 autoSizeTextType 只能用于 API LEVEL >= 26,并且 Android Studio 会显示有关该问题的烦人警告。

为了摆脱这个问题,我想以编程方式做到这一点,但有 3 种方法:

textView.setAutoSizeTextTypeUniformWithPresetSizes(...)
textView.setAutoSizeTextTypeUniformWithConfiguration(...)

这两个需要配置,但我想成为默认配置,与android:autoSizeTextType="uniform"相同。

textView.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM);

这说:

AppCompatTextView.setAutoSizeTextTypeWithDefaults can only be called from within the same library group (groupId=com.android.support).

PS:我将代码包装成:

if(android.os.Build.VERSION.SDK_INT= > 26) {...}

最佳答案

您可以使用TextViewCompat类来避免库限制错误。就像,

TextViewCompat.setAutoSizeTextTypeWithDefaults(textView,TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);

关于android - 如何以编程方式将 autoSizeTextType 统一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56251729/

相关文章:

java - Android TextView 上计数效果的 ValueAnimator

android - ConstraintLayout - 避免重叠

Swift - 当以编程方式设置字体大小并运行模拟器时,使用 Storyboard的属性文本功能全部重置

java - 初学者 Android : ListView to affect the next class

java - 底部导航图标颜色更改

android - Android声音重叠命令行ffmpeg不起作用

java - Android 缩放文本以适合

c# - 如何使用适配器将自定义字体设置为 ListView 项中的 TextView 之一?

java - Android 上 Knopflerfish 上的 Pax Web

Android textview 在包装时将单词分开