Android控件篇01之 TextView(三)跑马灯效果

本章介绍TextView跑马灯的使用和相关注意事项

点击查看"TextView示例源码"。

TextView的定义

<!-- 跑马灯效果 -->
<TextView
    android:id="@+id/tv_marque"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:singleLine="true"
    android:ellipsize="marquee" 
    android:marqueeRepeatLimit="marquee_forever"
    android:focusableInTouchMode="true"
    android:focusable="true"
    android:textSize="24dp"
    android:text="123+234-535435*28/234-345/2234/234123+234-535435*28/234-345/2234/234123+234-535435*28/234-345/2234/234+123+234-535435*28/234-345/2234/234+345982+34/543-89+797+46=135123" />

说明:

(01) android:singleLine="true" 是必须的。否则,一行显示不了的话;会多行显示。就没有跑马灯效果了!

(02) android:ellipsize="marquee" 是指定一行内容显示不下的情况下,使用跑马灯效果。

表达式 说明
android:ellipsize="start" 省略号在开头
android:ellipsize="middle" 省略号在中间
android:ellipsize="end" 省略号在结尾
android:ellipsize="marquee" 跑马灯显示

(03) android:marqueeRepeatLimit="marquee_forever" 在 android:ellipsize="marquee" 情况下使用,跑马灯无限循环。当然,这里的"marquee_forever"可以是整数,表示循环次数。

(04) android:focusableInTouchMode="true"和 android:focusable="true" 是为了让该TextView获取焦点。

注意:
1. 注意当TextView获取焦点,才能显示跑马灯效果。
2. 若没有添加android:focusableInTouchMode="true"和 android:focusable="true",而该TextView所属的layout中又有EditText或者ListView的话,则不会显示跑马灯效果。
3. 此外,不能有多个TextView同时跑马灯。同一时间,只有一个TextView能显示跑马灯效果!

by skywang
Previous     Next