首先,对于TextView来说,只有在内容超出能够显示的范围,才会需要滚动播放。
所以,我们要做的第一步就是设置范围:
android:singleLine="true"
设置单行显示,如果不设置
singleLine或者说设置
singleLine="false",TextView中的内容在满一行后就会自动换行。
接下来自然就会想到,我们既然设置了单行显示,那么超出的部分会怎么样呢?
那就要通过 android:ellipsize 这个属性来实现了:
android:ellipsize="start" -- <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">省略号在开头</span>
android:ellipsize="middle" -- <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">省略号在中间</span>
android:ellipsize="end" -- <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">省略号在结尾</span>
android:ellipsize="marquee" -- <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">跑马灯显示</span>
或者在程序中可通过setEillpsize显式设置。(注意:EditText不支持marquee这种模式)
如果设置了 marquee模式(跑马灯显式),就要记得设置 android:marqueeRepeatLimit 属性,也就是设置重复滚动的次数,当设置为marquee_forever时表示无限次。
最最重要的就是 android:focusable 属性(能否获得焦点),记得要设置为true;focusableInTouchMode是滑动时能否获得焦点,也要设置为true。
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:focusable="true"
android:text="dsadsakdhsajkhasdsad asd23a d132sa1d32as1d56sa4d65sa4d65sa465d4sadjksa"/>