在 RecyclerView 中自动调整 TextViews 会导致文本大小减小

Autosizing TextViews in RecyclerView causes text size to decrease

我正在尝试在 RecyclerView 中使用 Autosizing TextViews,但是当我滚动几次时,文本变得太小,显然无法正常工作。

我的 TextView 示例:

<android.support.v7.widget.AppCompatTextView
        android:id="@+id/textview_unit_title"
        android:layout_width="@dimen/tile_image_size"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:textSize="@dimen/medium_size"
        android:textColor="@color/color_text"
        android:paddingTop="@dimen/padding_title"
        android:layout_marginRight="2dp"
        android:layout_marginEnd="2dp"
        app:autoSizeMaxTextSize="@dimen/style_medium"
        app:autoSizeTextType="uniform"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/imageview_unit_icon"
        app:layout_constraintTop_toTopOf="parent"/>

我应该以编程方式在其他地方更新此缩放比例还是有其他解决方案?

Autosizing TextViews

Android 8.0 (API 级别 26) 允许您指示 TextView 让文本大小自动扩展或收缩以根据 TextView 的特性和边界填充其布局。

Note: If you set autosizing in an XML file, it is not recommended to use the value "wrap_content" for the layout_width or layout_height attributes of a TextView. It may produce unexpected results.

你应该限制高度

 android:layout_height="30dp"

我看到的问题是将您的视图高度设置为 wrap_content 允许文本大小变小,但文本永远不会再变大。这就是文档建议不要使用 wrap_content 作为视图大小的原因。但是,我发现如果您关闭自动调整大小,将文本大小设置为最大值,然后重新启用自动调整大小,文本大小将重置为最大大小并根据需要缩小。

所以我在 XML 中的视图如下所示:

<android.support.v7.widget.AppCompatTextView
    android:id="@+id/text_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:textAllCaps="true"
    android:textColor="@android:color/white"
    android:textSize="42sp"
    app:autoSizeMinTextSize="26dp"
    app:autoSizeMaxTextSize="42dp"
    app:autoSizeTextType="none"/>

然后在我的 ViewHolder 中,当我将文本绑定到视图时:

TextView title = view.findViewById(R.id.text_title);
String titleValue = "Some Title Value";

// Turn off auto-sizing text.
TextViewCompat.setAutoSizeTextTypeWithDefaults(title, 
    TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE);

// Bump text size back up to the max value.
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 42);

// Set your text as normal.
title.setText(titleValue);

// Post a runnable to re-enable auto-sizing text so that it occurs
// after the view is laid out and measured at max text size.
title.post(new Runnable() {
    @Override
    public void run() {     
        TextViewCompat
            .setAutoSizeTextTypeUniformWithConfiguration(title,
                    26, 42, 1, TypedValue.COMPLEX_UNIT_DIP);
    }
});

我把Michael Celey的答案打包成了class。参数app:autoSizeMinTextSizeapp:autoSizeMaxTextSizeapp:autoSizeTextType取自xml.

public class AutosizingTextView extends AppCompatTextView {

    private int minTextSize;
    private int maxTextSize;
    private int granularity;

    public AutosizingTextView(Context context) {
        super(context);
        init();
    }

    public AutosizingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AutosizingTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        minTextSize = TextViewCompat.getAutoSizeMinTextSize(this);
        maxTextSize = TextViewCompat.getAutoSizeMaxTextSize(this);
        granularity = TextViewCompat.getAutoSizeStepGranularity(this);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        // this method is called on every setText
        disableAutosizing();
        super.setText(text, type);
        post(this::enableAutosizing); // enable after the view is laid out and measured at max text size
    }

    private void disableAutosizing() {
        TextViewCompat.setAutoSizeTextTypeWithDefaults(this, TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE);
    }

    private void enableAutosizing() {
        TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(this,
                minTextSize, maxTextSize, granularity, TypedValue.COMPLEX_UNIT_PX);
    }
}```

Pavel Haluza 的回答方法很棒。然而,它并没有起作用,可能是因为他漏掉了一行 setTextSize(TypedValue.COMPLEX_UNIT_PX, maxTextSize);.

这是我的更新版本:

public class MyTextView extends AppCompatTextView {

private int minTextSize;
private int maxTextSize;
private int granularity;

public MyTextView(Context context) {
    super(context);
    init();
}

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MyTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init() {
    minTextSize = TextViewCompat.getAutoSizeMinTextSize(this);
    maxTextSize = TextViewCompat.getAutoSizeMaxTextSize(this);
    granularity = Math.max(1, TextViewCompat.getAutoSizeStepGranularity(this));
}

@Override
public void setText(CharSequence text, BufferType type) {
    // this method is called on every setText
    disableAutoSizing();
    setTextSize(TypedValue.COMPLEX_UNIT_PX, maxTextSize);
    super.setText(text, type);
    post(this::enableAutoSizing); // enable after the view is laid out and measured at max text size
}

private void disableAutoSizing() {
    TextViewCompat.setAutoSizeTextTypeWithDefaults(this, TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE);
}

private void enableAutoSizing() {
    TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(this,
            minTextSize, maxTextSize, granularity, TypedValue.COMPLEX_UNIT_PX);
}}

上述解决方案对我不起作用,所以这是我的

public class MyTextView extends AppCompatTextView {
    ...
    @Override
    public final void setText(CharSequence text, BufferType type) {
        // work around stupid auto size text not *growing* the font size we re binding in a RecyclerView if previous bind caused a small font
        int minTextSize = 0, maxTextSize = 0, granularity = 0;
        boolean doHack = TextViewCompat.getAutoSizeTextType(this) != TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE;
        if (doHack) {
            minTextSize = TextViewCompat.getAutoSizeMinTextSize(this);
            maxTextSize = TextViewCompat.getAutoSizeMaxTextSize(this);
            if (minTextSize <= 0 || maxTextSize <= minTextSize) { // better than validateAndSetAutoSizeTextTypeUniformConfiguration crashing
                if (BuildConfig.DEBUG)
                    throw new AssertionError("fix ya layout");
                doHack = false;
            } else {
                granularity = TextViewCompat.getAutoSizeStepGranularity(this);
                if (granularity < 0)
                    granularity = 1; // need this else setAutoSizeTextTypeUniformWithConfiguration barfs. TextView.UNSET_AUTO_SIZE_UNIFORM_CONFIGURATION_VALUE = 1.
                // make the TextView have 0 size so setAutoSizeTextTypeUniformWithConfiguration won't do calculations until after a layout pass using maxSize
                TextViewCompat.setAutoSizeTextTypeWithDefaults(this, TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE);
                setTextSize(TypedValue.COMPLEX_UNIT_PX, maxTextSize);
                measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY));
                setRight(getLeft());
                setBottom(getTop());
                requestLayout();
            }
        }
        super.setText(text, type);
        if (doHack)
            TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(this, minTextSize, maxTextSize, granularity, TypedValue.COMPLEX_UNIT_PX);
    }
    ...
}

只需 .setText("") 在重置您想要的文本大小之前。这确保您不会设置文本大小,然后立即使用 TextView 中的先前文本值自动调整大小。像这样:

TextView wordWordTextView = getView().findViewById(R.id.wordWordTextView);
wordWordTextView.setAlpha(0.0f);
wordWordTextView.setText("");
wordWordTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 50);
wordWordTextView.setText(wordStr);
wordWordTextView.animate().alpha(1.0f).setDuration(250);

我只是在 xml 文件中设置 android:maxLines="1",然后在 bindViewHolder

中编码
  TextViewCompat.setAutoSizeTextTypeWithDefaults(binding.tvResultExplain, TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE);
  binding.tvResultExplain.setText("");
  TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(binding.tvResultExplain, 12,
                16, 1, TypedValue.COMPLEX_UNIT_SP);
  binding.tvResultExplain.setText(item.getStatusExplain());

对我有用,也许它也能解决你的问题。