如何以编程方式将autoSizeTextType转换为uni?

时间:2019-05-22 07:44:53

标签: 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) {...}

1 个答案:

答案 0 :(得分:1)

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

TextViewCompat.setAutoSizeTextTypeWithDefaults(textView,TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM);
相关问题