问题
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 should not be -1. maxLines:1 lineCount:1
XML Code:
<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 Code:
public void setProgress() {
// setProgress() called every 0.5 seconds
// Hardcoded text
mTimeText.setText("0:05");
}
回答1:
Answering my own question.
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.
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.
The only other solution I could come up with is replacing android:maxLines="1"
with android:singleLine="true"
, however that xml property is deprecated!
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.
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.
回答2:
This is a bug in Android which is marked as fixed, so we'll have to wait for the next patch: https://issuetracker.google.com/issues/121092510
回答3:
I got it solved by changing the TextView's layout_height
configuration from wrap_content
to match_parent
. Defining a fixed layout_height
is another way to solve it.
Examples:
android:layout_height="wrap_content"
or android:layout_height="20dp"
来源:https://stackoverflow.com/questions/49416089/android-textview-getting-w-staticlayout-maxlineheight-should-not-be-1-maxli