Android TextView:设置文本时获取 "W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1"
Android TextView: Getting "W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1" when setting text
我根据计时器每 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_text
和 time_text
。
注释掉 //mTimeText.setText("0:05");
解决了警告消息被垃圾邮件发送的问题,所以我认为这个问题必须与 time_text
相关,但事实并非如此。
它与 title_text
有关。请注意我是如何设置属性 android:maxLines="1"
和 android:ellipsize="end"
的。如果我的文本会溢出 maxLines
限制并触发省略号,那么我会收到警告消息。删除行 android:ellipsize="end"
解决了这个问题。但是,我需要省略号,所以这是行不通的。
我能想出的唯一其他解决方案是用 android:singleLine="true"
替换 android:maxLines="1"
,但是 xml 属性 已弃用!
因此,我只是在 java 代码中以编程方式设置了 mTitleText.setSingleLine(true)
。该方法未被弃用,所以我想我是清楚的。
至于为什么注释掉 //mTimeText.setText("0:05");
阻止了警告信息的出现,我不太清楚。我被那个难住了。
我通过将 TextView 的 layout_height
配置从 wrap_content
更改为 match_parent
解决了这个问题。定义一个固定的layout_height
是另一种解决方法。
示例:
android:layout_height="wrap_content"
或 android:layout_height="20dp"
这是 Android 中的一个错误,已标记为已修复,因此我们必须等待下一个补丁:https://issuetracker.google.com/issues/121092510
我根据计时器每 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_text
和 time_text
。
注释掉 //mTimeText.setText("0:05");
解决了警告消息被垃圾邮件发送的问题,所以我认为这个问题必须与 time_text
相关,但事实并非如此。
它与 title_text
有关。请注意我是如何设置属性 android:maxLines="1"
和 android:ellipsize="end"
的。如果我的文本会溢出 maxLines
限制并触发省略号,那么我会收到警告消息。删除行 android:ellipsize="end"
解决了这个问题。但是,我需要省略号,所以这是行不通的。
我能想出的唯一其他解决方案是用 android:singleLine="true"
替换 android:maxLines="1"
,但是 xml 属性 已弃用!
因此,我只是在 java 代码中以编程方式设置了 mTitleText.setSingleLine(true)
。该方法未被弃用,所以我想我是清楚的。
至于为什么注释掉 //mTimeText.setText("0:05");
阻止了警告信息的出现,我不太清楚。我被那个难住了。
我通过将 TextView 的 layout_height
配置从 wrap_content
更改为 match_parent
解决了这个问题。定义一个固定的layout_height
是另一种解决方法。
示例:
android:layout_height="wrap_content"
或 android:layout_height="20dp"
这是 Android 中的一个错误,已标记为已修复,因此我们必须等待下一个补丁:https://issuetracker.google.com/issues/121092510