Is the xml attribute singleLine deprecated or not

2019-01-17 05:10发布

singleLine is/was used in xml layout files for TextView and EditText something like the following:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true" />

Some people on SO say singleLine is deprecated, while other people still suggest using it. Sometimes it even seems necessary to use when maxLines="1" doesn't work. (see here, here, and here)

The docs should be the place to go to answer this question, right? Here, they say:

This constant was deprecated in API level 3.

This attribute is deprecated. Use maxLines instead to change the layout of a static text, and use the textMultiLine flag in the inputType attribute instead for editable text views (if both singleLine and inputType are supplied, the inputType flags will override the value of singleLine).

However, in the TextView docs, there is no indication that it is deprecated, either for android:singleLine or for setSingleLine or for setTransformationMethod. The same TextView docs, by comparison, do state that other things like STATUS_BAR_HIDDEN and fitSystemWindows are deprecated. So is the singleLine deprecation an omission, was it "undeprecated", or what?

This question has been previously asked before but was not the main focus of the question (and was not answered).

10条回答
来,给爷笑一个
2楼-- · 2019-01-17 05:59

I have just added android:inputType="text"and removed android:maxLines="1", it worked fine for me.

查看更多
放我归山
3楼-- · 2019-01-17 06:05

Simple alternate way, use: android:maxLines="1"

查看更多
别忘想泡老子
4楼-- · 2019-01-17 06:06

The deprecated attribute was added in change d24b8183b9 which is nothing but a dump from Google's internal SCM:

auto import from //branches/cupcake/...@130745

As can be seen from the change core/res/res/values/attrs.xml diff adds the @deprecated doc comment, but core/java/android/widget/TextView.java diff does not alter anything from setSingleLine()'s doc comment.

Now without access to Google's internal SCM history, it is not possible to know what exactly caused above change in attrs.xml doc comment, but for your question

So is the singleLine deprecation an omission, was it "undeprecated", or what?

one possible answer is:TextView's single-line was neither deprecated, nor "undeprecated", but it was enhanced to take into account whether the view is editable, a password field or uses any other input type flag that affects single/multi-lineness.

查看更多
萌系小妹纸
5楼-- · 2019-01-17 06:07

Just thought I'll add that Android Studio 2.2.1 flags singleLine as deprecated. However, what I found is that in my instance:

android:singleLine="false"

works well, whereas

android:maxLines="2"

does not.

查看更多
萌系小妹纸
6楼-- · 2019-01-17 06:08

I think the answer to your question is already in one of the SO posts you linked to. Unfortunately, the deprecation of singleLines is not a black-or-white matter.

It is deprecated, but it is not going anywhere anytime soon.

It was deprecated because its performance is poor, relative to its successor, maxLines. It uses SingleLineTransformationMethod to replace newlines and carriage returns in the String you place in the TextView, unlike maxLines, which "just" adjusts the height of the TextView based on the number of lines and does no String replacing.

This method of replacing characters also meant that singleLine could break in unexpected ways (e.g. if you use custom fonts). It was these performance and reliability issues that led to its deprecation.

However, it is not going anywhere because, as the SO post you linked to states, it is still in use by many old Android applications, and it is still useful sometimes (e.g. when you want to show the entire text on one line and ignore carriage-returns and newlines).

Do note that deprecation does not necessarily mean that an API is going away. It just means that its use is discouraged, but may be permitted.

查看更多
Root(大扎)
7楼-- · 2019-01-17 06:08

singleLine IS deprecated. No discussion needed.

The only problem is the wrong documentation.
"use the textMultiLine flag in the inputType attribute instead for editable text views" is the direct opposite of what you want to achieve by using singleLine.

Instead of that you can use android:inputType="text".
For me it did exactly what I wanted - an edit with a single line without line breaks.

查看更多
登录 后发表回答