学而实习之 不亦乐乎

在 Android 中使用 TextView 实现文字走马灯效果

2023-12-12 20:46:45

对于长文本的显示,除了用省略号之外我们还可以让它滚动显示,这样的效果也叫走马灯效果。

我们实现这个效果,也要用到上面android:ellipsize 的属性,还有android:marqueeRepeatLimit,和android:focusable属性。

<TextView
    android:id="@+id/txtInfo"
    android:text="这里是测试内容,内容要长容要长要长长长长长长长长长长长长长长长长"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:focusable="true"
    android:focusableInTouchMode="true"  
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

如果实现这个走马灯效果时发现它跑不起来,记得把 android:focusableInTouchMode 属性添加上。