Autosize TextView 只变小不变大的问题
2020-11-09 本文已影响0人
gooddaytoyou
背景
android 提供 Autosizing TextViews
兼容配制
app:autoSizeMaxTextSize="30dp"
app:autoSizeMinTextSize="4dp"
app:autoSizeTextType="uniform"
问题描述
在使用的过程中,TextView的height设置成wrap_content
,显示的text size 只会变成的更小,从来不会再变大。
The issue I've seen with this is that setting your view height to be wrap_content allows the text size to get smaller, but the text will never get bigger again.
解决方案
1.TextView layout_height
设置成固定高度
2.通过设置Text时,动态改变autosize配制
public static void setAutoSizeText(final TextView textView, String content) {
final int minSize = TextViewCompat.getAutoSizeMinTextSize(textView);
final int maxSize = TextViewCompat.getAutoSizeMaxTextSize(textView);
//关闭autosize
TextViewCompat.setAutoSizeTextTypeWithDefaults(textView, TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE);
if (minSize <= 0 || maxSize <= 0) {
//说明没有配制相关的属性
textView.setText(content);
return;
}
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, TextViewCompat.getAutoSizeMaxTextSize(textView));
//开启autosize
textView.setText(content);
textView.post(new Runnable() {
@Override
public void run() {
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textView,
minSize, maxSize
, Math.max(1, TextViewCompat.getAutoSizeStepGranularity(textView)), TypedValue.COMPLEX_UNIT_PX);
}
});
}
相关资料
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview