【问题标题】:Given text can not be applied to TextView when TextView parameters are changed更改 TextView 参数时,给定文本无法应用于 TextView
【发布时间】:2020-03-22 07:56:02
【问题描述】:

所以我做了一个扩展来在 TextView 中实现预计算文本。

自从我迁移到 Material Components Theme 以来,我一直遇到错误。

这是下面的代码。

我还尝试将所有 TextView 和 AppCompatTextView 迁移到 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)

我的一些 TextView 具有这些属性

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

isSelected = true

【问题讨论】:

  • 我正在尝试重现这一点。你在使用 Theme.MaterialComponents.Light 吗?从实现“com.google.android.material:material:1.2.0-alpha05”?
  • 您是否尝试将textMetricsParamsCompat 更改为textMetricsParams? :)
  • @VytautasBerankis 我正在使用 1.1.0-beta02 意味着我们今天将发布更新并观察 rc02。该错误仅发生在特定设备上,特别是三星和运行 Android 9 的设备
  • 值得注意的是:像这样调用precomputedText.get() 违背了这样做的全部目的。你应该检查Future.get的文档
  • @Bobbobbington 您可以查看 Bahman 的答案。由于我在 RecyclerView 中,我在预计算文本之前检查了 Activity 是否仍然存在。

标签: android kotlin


【解决方案1】:

所有TextView 布局属性必须在创建参数之前设置 目的。如果在预计算期间更改它们,这可能会导致 a IllegalArgumentException 消耗预计算值时 在测量期间,并且不反映TextView的当前状态。

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

所以当你调用这个方法时它很重要:setTextFuture

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 2013-12-31
    • 2012-05-28
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多