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

时间:2018-06-05 09:06:35

标签: android textview android-textview-autosize

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

我的TextView示例:

<?php

if (isset($_GET['update'])) {

  $query = mysql_query("UPDATE iekartas SET
iernosauk='$Ierices_Nosaukums', tips='$Tips', razotajs='$Razotajs',
izgatdat='$Izgatavosanas_datums', adrese='$Adrese' where id='$Iekartas_ID'", $connection);
}
$query = mysql_query("select * from iekartas", $connection);
while ($row = mysql_fetch_array($query)) {
echo "<b><a href='iekartas.php?update={$row['Iekartas_ID']}'>{$row['Ierices_Nosaukums']}</a></b>";
echo "<br />";

}
?>

<tr>
    <form action="ierices.php" method="post">
        <td><input class="checkbox" type="checkbox" id="<?php echo $row['Iekartas_ID'] ?>" name="id[]"></td>
        <td> <input type="text" name="iernosauk" value=" <?php echo $row["Ierices_Nosaukums"]; ?>"></td>
        <td> <input type="text" name="tips" value=" <?php echo $row["Tips"]; ?>"></td>
        <td> <input type="text" name="razotajs" value=" <?php echo $row["Razotajs"]; ?>"></td>
        <td> <input type="text" name="izgatdat" value=" <?php echo $row["Izgatavosanas_datums"]; ?>"></td>
        <td> <input type="text" name="adrese" value=" <?php echo $row["Adrese"]; ?>"></td>
        <td> <input type="hidden" name="id" value=" <?php echo $row["Iekartas_ID"]; ?>"></td>

<?php
    $i++;
}
?>

        <button type="button" action="update.php" name="updatedb" class="btn btn-danger" id="updatedb" value=update>Atjaunot</button>
</tr>
    </form>

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

6 个答案:

答案 0 :(得分:6)

Autosizing TextViews

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

  

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

你应该绑定高度

 android:layout_height="30dp"

答案 1 :(得分:6)

我看到的一个问题是,将视图高度设置为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);
    }
});

答案 2 :(得分:3)

我将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);
    }
}```

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

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

答案 5 :(得分:0)

只需.setText(“”)即可重置所需的文本大小。这样可以确保您没有设置textsize,然后立即使用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);