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

2024-03-06

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

W/StaticLayout: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");
}

回答我自己的问题。

注意我有两个 TextViewtitle_text and time_text。 评论出来//mTimeText.setText("0:05");解决了警告消息被垃圾邮件发送的问题,所以我认为这个问题与time_text,但事实并非如此。

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

我能想到的唯一其他解决方案是替换android:maxLines="1" with android:singleLine="true",但是该 xml 属性已被弃用!

因此,我只是设置mTitleText.setSingleLine(true)在我的java代码中以编程方式。该方法并未被弃用,所以我认为我是清楚的。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐