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条回答
Luminary・发光体
2楼-- · 2019-01-17 06:09

In the official grepcode of TextView (v5.1.0 r1) :

android:singleLine is not annotated with @Deprecated.

I also see this in setInputType method:

boolean singleLine = !isMultilineInputType(type);

// We need to update the single line mode if it has changed or we
// were previously in password mode.
if (mSingleLine != singleLine || forceUpdate) {
    // Change single line mode, but only change the transformation if
    // we are not in password mode.
    applySingleLine(singleLine, !isPassword, true);
}

setInputType overrides the mSingleLine value so.

EDIT : This xml attribute is now officially deprecated. (since API 3?). It is now visible in AndroidStudio xml editor.

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

While Android Studio claims that it is deprecated, in reality we had an edit text which should allow itself to be only single-line.

Adding maxLines="1" made it allow newline characters, which is not suitable for our needs.

So we went back to using singleLine="true".

查看更多
神经病院院长
4楼-- · 2019-01-17 06:16

Just for adding some more information to the discussion, Lint now has the following error:

"Combining ellipsize and maxLines=1 can lead to crashes on some devices. Earlier versions of lint recommended replacing singleLine=true with maxLines=1 but that should not be done when using ellipsize.

More info: https://issuetracker.google.com/issues/36950033"

So, I guess that singleLine is now, and I think we should come up with a new term... "deprecatedish"?

查看更多
\"骚年 ilove
5楼-- · 2019-01-17 06:16

Only providing android:maxLines="1" and android:minLines="1" wont solve the issue of keyboard actionNext related issue. Use android:inputType="text" for the same.

查看更多
登录 后发表回答