在RecyclerView中自适应大小的TextView会导致文本大小减小。

18

我正在尝试在RecyclerView中使用自动调整大小的TextView,但当我滚动几次后,文本会变得非常小,显然不能正常工作。

我的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"/>

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

7个回答

24

我遇到的问题是,将视图高度设置为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:谢谢你提供的解决方案。我的问题是在哪里使用你的解决方案?所有我将高度设置为“wrap-content”的地方,还是只在RecyclerView中使用?因为对于我来说,在使用高度为“wrap-content”时,我同时使用了minHeight和MaxHeight。这个方法已经很好用了,但它不能在RecyclerView中正常工作。但是当我滚动屏幕时,它就可以工作了。 - Akash Patra

15

自适应大小的TextView

从Android 8.0(API level 26)开始,您可以指示TextView根据其特征和边界来自动扩展或收缩文本大小以填充其布局。

注意:如果您在XML文件中设置了自适应大小,则不建议在TextView的layout_width或layout_height属性中使用"wrap_content"。这可能会产生意外的结果。

您应当限制高度。

 android:layout_height="30dp"

16
好的,这是一个限制。 - Softlion

8

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);
}}

运行得非常好! 我有一个要求,即maxLines=2。因此,我必须将其高度设置为WRAP_CONTENT。传统的AppCompatTextView没有按预期工作。但是这个自定义textview完成了工作。 - Maulik Hirani
顺便说一句 - 我还添加了这行代码到setText()方法中,以确保如果文本没有改变,我们不会重置所有内容:if (getText().equals(text)) return; - jpage4500
然而,这里有一个小问题。当文本被更改时,它似乎会闪烁。你会看到字母变得巨大(无论最大尺寸设置为多少),然后迅速缩小。 - jpage4500

7

我将Michael Celey的回答打包成一个类。参数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);
    }
}```

2
在禁用调整大小后,请不要忘记将文本设置回最大尺寸。如果您没有从缩放后开始,它偶尔会以较小的尺寸启动。感谢您使这更加简洁! - Michael Celey

1

以上的解决方案对我没有用,所以这是我的解决方案

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);
    }
    ...
}

0

我只是在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());

这对我有效,也许它也可以解决你的情况。


0
在重置文本视图的文本大小之前,只需使用 .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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接