代码之家  ›  专栏  ›  技术社区  ›  mco

Android TextView:设置文本时获取“W/StaticLayout:maxLineHeight不应为-1。maxLines:1 lineCount:1”

  •  24
  • mco  · 技术社区  · 8 年前

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

    带静态布局:maxLineHeight不应为-1。最大行数:1行计数: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");
    }
    
    3 回复  |  直到 6 年前
        1
  •  23
  •   mco    6 年前

    回答我自己的问题。

    注意我有两个TextView title_text time_text 。 注释掉 //mTimeText.setText("0:05"); 解决了警告消息被垃圾邮件发送的问题,因此我认为该问题与 time\u文本 ,但事实并非如此。

    这与 title\u文本 。注意我是如何设置属性的 android:maxLines="1" android:ellipsize="end" 。如果我有文本会溢出 maxLines 限制并触发省略号,然后我会得到警告消息。删除线路 android:ellipsize=“结束” 解决了问题。然而,我需要椭圆,所以这行不通。

    我唯一能想到的另一个解决方案是更换 android:maxLines=“1” 具有 android:singleLine="true" ,但不推荐使用该xml属性!

    因此,我刚刚设置 mTitleText.setSingleLine(true) 以编程方式在我的java代码中。这种方法没有遭到反对,所以我想我是清楚的。

    至于为什么要发表评论 //多行文字。setText(“0:05”); 阻止了警告信息出现,我真的不知道。我在那件事上被难住了。

        2
  •  5
  •   drc    7 年前

    这是Android中的一个bug,标记为已修复,因此我们必须等待下一个补丁: https://issuetracker.google.com/issues/121092510

        3
  •  3
  •   Jordao Serafim    7 年前

    我通过改变文本视图的 layout_height 配置自 wrap_content match_parent 。定义固定 layout\u高度 是另一种解决方法。


    示例:

    android:layout_height="wrap_content" android:layout_height="20dp"