安卓 TextView : Getting "W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1" when setting text

标签 android textview

我根据计时器每 0.5 秒在 TextView 上设置一些文本。每次,当计时器运行并设置文本时,我都会在控制台中收到此警告消息。

W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1

XML 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:layout_marginTop="12dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:textColor="@color/white"/>

    <TextView
        android:id="@+id/time_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingEnd="4dp"
        android:paddingStart="12dp"
        android:textColor="@color/white"
        android:textSize="12sp"
        tools:text="0:00" />
</RelativeLayout>

Java 代码:

public void setProgress() {
    // setProgress() called every 0.5 seconds
    // Hardcoded text
    mTimeText.setText("0:05");
}

最佳答案

回答我自己的问题。

注意我有两个 TextView 的 title_texttime_text。 注释掉 //mTimeText.setText("0:05"); 解决了警告消息被垃圾邮件的问题,所以我认为这个问题必须与 time_text ,但它没有。

它与 title_text 有关。注意我是如何设置属性 android:maxLines="1"android:ellipsize="end" 的。如果我的文本会溢出 maxLines 限制并触发省略号,那么我会收到警告消息。删除行 android:ellipsize="end" 解决了这个问题。但是,我需要省略号,所以这行不通。

我能想到的唯一其他解决方案是将 android:maxLines="1" 替换为 android:singleLine="true",但是不推荐使用该 xml 属性!

因此,我只是在我的 java 代码中以编程方式设置了 mTitleText.setSingleLine(true)。这种方法没有被弃用,所以我想我很清楚。

至于为什么注释掉 //mTimeText.setText("0:05"); 会阻止显示警告消息,我真的不知道。我被那个难住了。

关于安卓 TextView : Getting "W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1" when setting text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49416089/

相关文章:

android - Kotlin:将 Activity 的子类声明为静态

java - Android - 如何使 View 仅填充屏幕的其余部分

android - android 股票浏览器上的 flexbox

android - 如何在 RelativeLayout 中的 TextView 中设置文本的正确位置?

Android:更改 ListView 内的 TextView 的文本?

android微调器下拉复选框

java - Android - 需要有关 ondraw() 查看示例的帮助

ios - 如何在 UITextView 的文本后面动态创建多个按钮

android - 如何设置 autoLink ="web"链接的样式?

java - 使用开放套接字发送纬度和经度