TextView 跑马灯去掉左右阴影

6 阅读1分钟

在 Android 开发中,高频使用控件 TextView 的跑马灯效果也是经常使用的,下面是一个实际的使用示例:

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/dp_8"
    android:layout_marginEnd="@dimen/dp_18"
    android:layout_weight="1"
    style="@style/marquee_text"
    android:fadingEdge="none"
    android:requiresFadingEdge="none"
    android:textSize="@dimen/sp_12"/>

跑马灯属性设置

<style name="marquee_text">
    <item name="android:singleLine">true</item>
    <item name="android:ellipsize">marquee</item>
    <item name="android:marqueeRepeatLimit">marquee_forever</item>
    <item name="android:scrollHorizontally">true</item>
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
    <item name="android:textSize">13sp</item>
    <item name="android:textColor">#151517</item>
</style>

把上面的代码放进项目中使用,你发现跑马灯没效果,那么,还需要设置另外一个属性:

tvContent?.isSelected = true

这下,可以跑起来了,但我发现它好像左右边有阴影,那么该如何让阴影消除呢?或者就这么带着感觉更好,下面是把阴影消除的方法:

tvContent?.isHorizontalFadingEdgeEnabled = false
tvContent?.isVerticalFadingEdgeEnabled = false
tvContent?.setFadingEdgeLength(0)

快去试试效果吧!!!