其实实现跑马灯并没有大家想想的那么难,Android原生控件Textview即可实现;很简单只需要一个Textview。
下面就是代码:
<TextView
android:layout_width="60dp"
android:layout_height="20dp"
android:textColor="@android:color/black"
android:ellipsize="marquee"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:text="这是跑马灯的效果这是跑马灯的效果"/>
其中 注意:
1.高度跟宽度不能设置成wrap_content,否则就会将全部内容都显示出来
2..android:singleLine="true" 设置成单行模式
3.ellipsize:Textview的这个属性指的是Textview的省略方式
android:ellipsize = "end" 省略号在结尾
android:ellipsize = "start" 省略号在开头
android:ellipsize = "middle" 省略号在中间
android:ellipsize = "marquee" 跑马灯
android:scrollHorizontally="true"这个属性设置的事滚动方向:水平滚动
android:marqueeRepeatLimit="marquee_forever" 这个属性是循环次数 ,这里设置的无限次循环
剩下的就是Textview的获取焦点:要将它的获取焦点均设为true,才能管用。
android:focusable="true"
android:focusableInTouchMode="true"
其他的属性大家可以随意加,但是这几项必须有。这只是简单跑马灯效果,不能控制时间,不能控制循环速度。
其实大家可以尝试 自定义Textview ,有的大神写过,也看过,大家有兴趣可以尝试一下。
欢迎提出质疑,请多指教!