如何以编程方式使 autoSizeTextType 统一?

How to autoSizeTextType to unifrom programatically?

可以这样做

<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 class 来避免库限制错误。喜欢,

TextViewCompat.setAutoSizeTextTypeWithDefaults(textView,TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM);