Android TextView:"W/StaticLayout:maxLineHeight"不应为-1.maxLines:1 lineCount:1“设置文字时 [英] Android TextView: Getting "W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1" when setting text

查看:184
本文介绍了Android TextView:"W/StaticLayout:maxLineHeight"不应为-1.maxLines:1 lineCount:1“设置文字时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm setting some text on a TextView every 0.5 seconds based on a timer. Everytime, when the timer runs and the text is set, I'm getting this warning message being spammed in my console.

W/StaticLayout:maxLineHeight不应为-1.maxLines:1 lineCount:1

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_text time_text .注释掉//mTimeText.setText("0:05"); 可以解决警告消息被发送为垃圾邮件的问题,因此我认为该问题必须与 time_text 相关联,但是没有.

Notice how I have two TextView's title_text and time_text. Commenting out //mTimeText.setText("0:05"); solved the issue of the warning message being spammed, so I thought the issue had to do something with time_text, but it didn't.

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

It had to do with title_text. Notice how I set the properties android:maxLines="1" and android:ellipsize="end". If I had text that would overflow past the maxLines limit and trigger the ellipses, then I would get the warning message. Removing the line android:ellipsize="end" solved the issue. However, I need the ellipses, so that's not going to work.

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

The only other solution I could come up with is replacing android:maxLines="1" with android:singleLine="true", however that xml property is deprecated!

因此,我只需在Java代码中以编程方式设置 mTitleText.setSingleLine(true).该方法没有被弃用,所以我认为我很清楚.

Therefore, I just set mTitleText.setSingleLine(true) programmatically in my java code. That method isn't deprecated so I think I'm in the clear.

关于为什么注释掉//mTimeText.setText("0:05"); 导致警告消息无法显示的原因,我真的不知道.我被那个绊倒了.

As to why commenting out //mTimeText.setText("0:05"); prevented the warning message from showing up, I don't really know. I'm stumped on that one.

这篇关于Android TextView:"W/StaticLayout:maxLineHeight"不应为-1.maxLines:1 lineCount:1“设置文字时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆