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

标签 android textview android-textview-autosize

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

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

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

最佳答案

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

所以我的 XML View 看起来像:

<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 中,当我将文本绑定(bind)到 View 时:

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

关于android - 在 RecyclerView 中自动调整 TextViews 会导致文本大小减小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50696258/

相关文章:

android - 超时定位

android - LayerDrawable转位图

android - 在 TextView 上设置文本时,在屏幕上显示文本的时间太长

java - 如何在listview android中使文本颜色透明?

android - 如何将 TextViewCompat 用于 API 26 以下的文本自动调整大小?

android - 使用自动调整大小时无法以编程方式更改文本大小

android - 如何在android中查找网络启用状态?

java - Android 下载监听器

android - TextView 宽度匹配drawableTop 宽度