首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自动调整TextViews在RecyclerView中的大小会导致文本大小减小

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

Stack Overflow用户
提问于 2018-06-05 09:06:34
回答 7查看 7.1K关注 0票数 15

我试图在自动定径TextViews中使用RecyclerView,但是当我滚动几次时,文本变得非常小,显然不能正常工作。

我的TextView示例:

代码语言:javascript
运行
复制
<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"/>

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

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2018-06-05 09:41:25

自动定径TextViews

Android8.0(APILevel26)允许您指示TextView根据TextView的特性和边界,让文本大小自动展开或收缩,以填充其布局。

注意:如果在XML文件中设置自动调整大小,则不建议将"wrap_content“值用于TextView的layout_width或layout_height属性。它可能会产生意想不到的结果。

你应该把高度绑起来

代码语言:javascript
运行
复制
 android:layout_height="30dp"
票数 15
EN

Stack Overflow用户

发布于 2018-10-12 22:13:56

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

因此,我的XML视图如下所示:

代码语言:javascript
运行
复制
<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中,当我将文本绑定到视图时:

代码语言:javascript
运行
复制
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);
    }
});
票数 22
EN

Stack Overflow用户

发布于 2019-05-16 09:39:36

帕维尔·哈鲁扎的回答非常好。然而,它没有起作用,可能是因为他错过了一条线setTextSize(TypedValue.COMPLEX_UNIT_PX, maxTextSize);

以下是我的最新版本:

代码语言:javascript
运行
复制
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);
}}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50696258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档