Android 跑马灯 简单实现

首先,我们如果想让TextView实现跑马灯效果的话,
我们只需要关注5个属性即可
1.要单行显示
2.单行显示多余部分如何显示,即 ellipsize 属性的设置
3.获取焦点
4.走马灯重复次数
5.强制的获得了焦点,让 View 响应所有的 touch 事件

如果你仅仅只想实现一行跑马灯效果的话 那么我们只需在布局文件设置一个TextView,将跑马灯所需的属性配置即可
具体代码实现如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@color/colorAccent"
          android:ellipsize="marquee"
          android:focusable="true"
          android:focusableInTouchMode="true"
          android:marqueeRepeatLimit="marquee_forever"
          android:singleLine="true"
          android:text="来吧,朋友 ,看一下这个效果如何,Android之跑马灯"
          android:textColor="#ffffff"
          />


</RelativeLayout>

在我们单行的跑马灯效果还是可以的,但是我们如果要实现多行跑马灯,
你可能会说,我们写两个TextView不就好了吗,如果这也操作的话便会出现一个Bug,在你运行时 你会发现只有第一行会动.

那么你如果要实现多行跑马灯的效果的话可点击下方链接地址:
https://blog.csdn.net/LZ0419/article/details/83993707
在这篇文章里我们解决了焦点抢占的问题

猜你喜欢

转载自blog.csdn.net/LZ0419/article/details/83992149