Android singleLine弃用,怎么正确处理?

一、首先看一下singleLine的解释与用法

android:singleLine
Formats: boolean
Constrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines, and advances focus instead of inserting a newline when you press the enter key. The default value is false (multi-line wrapped text mode) for non-editable text, but if you specify any value for inputType, the default is true (single-line input field mode). {@deprecated 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). }

SingleLine的简单解释:
没设置singleLine时,也就是默认false时,文字到达末尾时再输入字符会折行,变成多行。按下回车时,插入新行。
设置singleLine后,会限制文本成一行,可横向滚动。当你按下回车键时,会将焦点移向下一个组件。

SingleLine已被弃用:

  1. 对于静态文本,如TextView,请使用maxLines代替
  2. 对于可编辑的组件,如EditText,请使用inputType = “textMultiLine” 代替

singleLine默认为false,但若设置inputType属性为任意一个值,singleLine的值默认变为true。

二、singleLine弃用的处理

可以使用下面的方法替代singleLine:

<TextView
    android:maxLines="1"
    />
<!-- 单行 -->    
<EditText
    android:inputType="text"
    />
<!-- 多行 -->    
<EditText
    android:inputType="textMultiLine"
    />

三、注意事项

实际使用中,如果TextView设置了固定宽高,此时只设置maxLines=1,当内容超出了组件大小,依然会换行显示。

<TextView
	android:layout_width="200dp"
    android:layout_height="40dp"
    android:maxLines="1"/>

配合 ellipsize 使用时,就没问题了:

<TextView
	android:layout_width="200dp"
    android:layout_height="40dp"
    android:maxLines="1"
    android:ellipsize="end"
/>
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洛克Lee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值