Данный текст не может быть применен к TextView при изменении параметров TextView. - PullRequest
1 голос
/ 22 марта 2020

Итак, я сделал расширение для реализации предварительно вычисленного текста в TextView.

Хотя с тех пор, как я перешел на тему компонентов материалов, я получаю ошибки.

Вот код ниже.

Я также попытался перенести все TextViews и AppCompatTextViews в MaterialTextView. Тем не менее проблема все еще возникает.

fun AppCompatTextView.setTextFuture(string: String?) {
    val precomputedText =
        PrecomputedTextCompat.getTextFuture(
            string ?: "",
            TextViewCompat.getTextMetricsParams(this),
            null
        )

    textMetricsParamsCompat = precomputedText.get().params
    setTextFuture(precomputedText)
    text = string

}

fun AppCompatTextView.setTextFuture(charSequence: CharSequence?) {
    val precomputedText =
        PrecomputedTextCompat.getTextFuture(
            charSequence ?: "",
            TextViewCompat.getTextMetricsParams(this),
            null
        )

    textMetricsParamsCompat = precomputedText.get().params
    setTextFuture(precomputedText)
    text = charSequence
}

fun AppCompatTextView.setTextFuture(stringResId: Int) {
    val string = context.getString(stringResId)

    val precomputedText =
        PrecomputedTextCompat.getTextFuture(
            string,
            TextViewCompat.getTextMetricsParams(this),
            null
        )

    textMetricsParamsCompat = precomputedText.get().params
    setTextFuture(precomputedText)
    text = string
}

Ошибки были следующие.

Fatal Exception: java.lang.IllegalArgumentException
Given text can not be applied to TextView.
androidx.core.widget.TextViewCompat.setPrecomputedText (TextViewCompat.java:891)
androidx.appcompat.widget.AppCompatTextView.onMeasure (AppCompatTextView.java:550)
android.view.View.measure (View.java:24953)
Fatal Exception: java.lang.IllegalArgumentException
PrecomputedText's Parameters don't match the parameters of this TextView.Consider using setTextMetricsParams(precomputedText.getParams()) to override the settings of this TextView: PrecomputedText: {textSize=49.0, textScaleX=1.0, textSkewX=0.0, letterSpacing=0.03125, textLocale=[en_GB], typeface=android.graphics.Typeface@5e608edd, variationSettings=null, elegantTextHeight=false, textDir=android.text.TextDirectionHeuristics$TextDirectionHeuristicInternal@eb941bf, breakStrategy=1, hyphenationFrequency=0}TextView: {textSize=49.0, textScaleX=1.0, textSkewX=0.0, letterSpacing=0.03125, textLocale=[en_GB], typeface=android.graphics.Typeface@5e608edd, variationSettings=null, elegantTextHeight=false, textDir=android.text.TextDirectionHeuristics$TextDirectionHeuristicInternal@eb941bf, breakStrategy=1, hyphenationFrequency=0}
android.widget.TextView.setText (TextView.java:6724)
Fatal Exception: java.lang.IllegalArgumentException
PrecomputedText's Parameters don't match the parameters of this TextView.Consider using setTextMetricsParams(precomputedText.getParams()) to override the settings of this TextView: PrecomputedText: {textSize=70.0, textScaleX=1.0, textSkewX=0.0, letterSpacing=0.03125, textLocale=[en_CA], typeface=android.graphics.Typeface@ab2ab1bb, variationSettings=null, elegantTextHeight=false, textDir=android.text.TextDirectionHeuristics$TextDirectionHeuristicInternal@34fe469, breakStrategy=1, hyphenationFrequency=0}TextView: {textSize=70.0, textScaleX=1.0, textSkewX=0.0, letterSpacing=0.03125, textLocale=[en_CA], typeface=android.graphics.Typeface@ab2ab1bb, variationSettings=null, elegantTextHeight=false, textDir=android.text.TextDirectionHeuristics$TextDirectionHeuristicInternal@34fe469, breakStrategy=1, hyphenationFrequency=0}
android.widget.TextView.setText (TextView.java:6731)

Некоторые из моих TextViews имеют эти свойства

android:ellipSize="marquee"
android:singleLine="true"

isSelected = true

1 Ответ

1 голос
/ 06 апреля 2020

все TextView свойства макета должны быть установлены до создания объекта Params. Если они изменяются во время предварительного вычисления, это может вызвать IllegalArgumentException, когда предварительно вычисленное значение используется во время измерения , и не отражает текущее состояние TextView.

https://developer.android.com/reference/androidx/core/text/PrecomputedTextCompat

, поэтому важно при вызове этого метода: setTextFuture

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...