1、基于开发者文档的官方说明

跑马灯效果主要使用的控件为TextView,其中涉及的几个标签如下所示:

android:ellipsize
If set, causes words that are longer than the view is wide to be ellipsized instead of broken in the middle. You will often also want to set scrollHorizontally or singleLine as well so that the text as a whole is also constrained to a single line instead of still allowed to be broken onto multiple lines.
(如果设置,则会导致比视图长的单词被省略,而不是在在中间被打断。您通常还需要设置scrollHorizontal或singleLine,以便文本作为一个整体也被限制为单行,而不是仍然被允许拆分为多行。)

java代码中对应的方法:setEllipsize(TextUtils.TruncateAt)

    /*** Causes words in the text that are longer than the view's width* to be ellipsized instead of broken in the middle.  You may also* want to {@link #setSingleLine} or {@link #setHorizontallyScrolling}* to constrain the text to a single line.  Use <code>null</code>* to turn off ellipsizing.*/public void setEllipsize(TextUtils.TruncateAt where) {// TruncateAt is an enum. != comparison is ok between these singleton objects.if (mEllipsize != where) {mEllipsize = where;if (mLayout != null) {nullLayouts();requestLayout();invalidate();}}}//TruncateAt包含的属性值public enum TruncateAt {START,MIDDLE,END,MARQUEE,/*** @hide*/@UnsupportedAppUsageEND_SMALL}

android:focusableInTouchMode:布尔值,用于控制视图在触摸模式下是否可以对焦
android:singleLine:将文本限制为单个水平滚动行(当前已弃用,推荐使用maxLines)
android:clickable:定义此视图是否对单击事件做出反应

2、xml文件的方式

 <!--纯xml文件,需要点击后才能开始跑马灯的效果--><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/horse_race_lamp_value"android:textColor="@color/teal_700"android:ellipsize="marquee"android:focusableInTouchMode="true"android:singleLine="true"android:clickable="true"android:textSize="30sp"/>

3、xml文件+代码控制

    <!--使用xml文件+代码控制--><TextViewandroid:id="@+id/tv_horse_lamp"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/horse_race_lamp_value"android:singleLine="true"android:textSize="30sp"android:textColor="@color/purple_200"/>
    @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_horse_race_lamp);//通过代码控制,获取控件,添加mHorseLampTv = findViewById(R.id.tv_horse_lamp);mHorseLampTv.setEllipsize(TextUtils.TruncateAt.MARQUEE);mHorseLampTv.setSelected(true);}
//针对setSelected的源码解释,可以看到此方法中针对Ellipsize属性判断是否为MARQUEE(走马灯的条件之一)@Overridepublic void setSelected(boolean selected) {boolean wasSelected = isSelected();super.setSelected(selected);if (selected != wasSelected && mEllipsize == TextUtils.TruncateAt.MARQUEE) {if (selected) {startMarquee();} else {stopMarquee();}}}

4、xml文件+自定义TextView控件

package com.example.clientapplication;import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;import androidx.annotation.NonNull;
import androidx.annotation.Nullable;//自定义控件
public class MyHorseRaceLampView extends androidx.appcompat.widget.AppCompatTextView {public MyHorseRaceLampView(@NonNull Context context) {super(context);}public MyHorseRaceLampView(@NonNull Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public MyHorseRaceLampView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overridepublic boolean isFocused() {return true;}
}
    <!--使用自定义控件--><com.example.clientapplication.MyHorseRaceLampViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/horse_race_lamp_value"android:textColor="@color/cardview_dark_background"android:singleLine="true"android:ellipsize="marquee"android:textSize="30sp"/>

5、全部代码

  • activity_horse_race_lamp.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"tools:context=".HorseRaceLampActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="走马灯效果"android:gravity="center"android:textSize="40sp"/>
<!--纯xml文件--><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/horse_race_lamp_value"android:textColor="@color/teal_700"android:ellipsize="marquee"android:focusableInTouchMode="true"android:singleLine="true"android:clickable="true"android:textSize="30sp"/><TextViewandroid:id="@+id/tv_horse_lamp"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/horse_race_lamp_value"android:singleLine="true"android:textSize="30sp"android:textColor="@color/purple_200"/><!--使用自定义控件--><com.example.clientapplication.MyHorseRaceLampViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/horse_race_lamp_value"android:textColor="@color/cardview_dark_background"android:singleLine="true"android:ellipsize="marquee"android:textSize="30sp"/>
</LinearLayout>
  • HorseRaceLampActivity
package com.example.clientapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;public class HorseRaceLampActivity extends AppCompatActivity {private TextView mHorseLampTv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_horse_race_lamp);//通过代码控制mHorseLampTv = findViewById(R.id.tv_horse_lamp);mHorseLampTv.setEllipsize(TextUtils.TruncateAt.MARQUEE);mHorseLampTv.setSelected(true);}
}
  • MyHorseRaceLampView
package com.example.clientapplication;import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;import androidx.annotation.NonNull;
import androidx.annotation.Nullable;public class MyHorseRaceLampView extends androidx.appcompat.widget.AppCompatTextView {public MyHorseRaceLampView(@NonNull Context context) {super(context);}public MyHorseRaceLampView(@NonNull Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public MyHorseRaceLampView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overridepublic boolean isFocused() {return true;}
}

6、效果展示

Android --- 跑马灯效果相关推荐

  1. android跑马灯效果不起作用,Android跑马灯效果失效问题

    Android中的跑马灯效果在特定情况下用的效果很不错,调试的时候发现在2.3系统下,文字跑动正常,后来无意换了另一个手机,4.0以上的系统,发现文字跑动效果失效,研究后发现有两种情况会导致失效. 先 ...

  2. android 跑马灯效果

    修改界面跳转后跑马灯线程停止问题 package com.sunarvr.artist.view;import android.content.Context; import android.cont ...

  3. android跑马灯效果横向,Android自定义View实现纵向跑马灯效果详解

    首先看看效果图(录制的gif有点卡,真实的效果还是很流畅的) 实现思路 通过上面的gif图可以得出结论,其实它就是同时绘制两条文本信息,然后通过动画不断的改变两条文本信息距离顶部的高度,以此来实现滚动 ...

  4. Android跑马灯效果

    Android在有时候在显示文字时候宽度不够,换行感觉不好看有点别扭影响整体美观,于是就想让其滚动显示. 最通用的做法就是写个通用的样式文件: <style name="marquee ...

  5. Android实现跑马灯效果及问题解决

    今天公司项目让实现一个跑马灯的效果,因为有用户反映总是忘记还款截至时间,于是就要求做一个单条文字横向滚动的效果,用于提示用户. GitHub上找了几个跑马灯的库,本以为可以实现自己想要的效果,不过这个 ...

  6. Android 使用 ellipsize 实现文字横向移动效果(跑马灯效果)

    实现的效果图如下 ellipsize 可以设置跑马灯效果 具体代码设置如下 <TextViewandroid:layout_width="match_parent"andro ...

  7. android:ellipsize实现跑马灯效果总结

    原文地址:http://www.cnblogs.com/Gaojiecai/archive/2013/06/18/3142783.html android:ellipsize用法如下: 在xml中 a ...

  8. Android 文本实现跑马灯效果 用自带的TextView控件

    注意跑马灯需要文本已经确定的情况下设置 1.第一种方式在布局代码中 <TextViewandroid:id="@+id/music_name_tv"android:layou ...

  9. Android TextView跑马灯效果与设置文字阴影

    TextView跑马灯效果的实现 自定义 一个TextView public class MusicDesText extends TextView {public MusicDesText(Cont ...

最新文章

  1. 2018年《环球科学》十大科学新闻出炉:霍金逝世、贺建奎事件位列前二
  2. python学习之-- mysql模块和sqlalchemy模块
  3. 三步更改win7开机密码
  4. c#如何wmf图片转换成png图片_每日一学:如何将png图片转换为jpg图片
  5. 从cpp向qml文件传中文字符串的方法
  6. WP7基础学习---第九讲
  7. 多股回测(backtrader+quantstats+akshare)
  8. python中的自测语句是什么?
  9. python到底是啥_Python语言中的__init__到底是干什么的?
  10. PencilWang博客目录
  11. [转载] 【Python】set() 集合操作与运算 元素输出顺序
  12. T^T找数字(搜索+二进制枚举)
  13. 论一个蒟蒻的脑子里可以有多少坑(貌似咕了……目前更新保持在noip阶段)
  14. 如何快速获得Q币(python简单实现)
  15. SpringCloud版本Hoxton SR5 --- 第五讲:zuul 路由、过滤、容错与回退、集群、高可用
  16. Python自动化构建雷电模拟器
  17. 判断类型是否继承_读《Java核心技术》-继承(覆盖、final、强制类型转换、抽象类)...
  18. 基于matlab的DTMF信号的产生和检测(1)
  19. 解决win7 处理器,安装内存不可用
  20. kingcms php 下载,KingCMS PHP 2009版盛大发布

热门文章

  1. **Java有哪些悲观锁的实现_淘宝Java研发面试:Redis+Mybatis+高并发+线程池
  2. python爬取mysql_如何利用 Python+MySQL 存储爬取的币乎数据
  3. slider unity 头顶血条_Unity开发者教程:人物血条跟随的功能开发(一)
  4. python自动剪视频_Pythonmoviepy一个快速视频剪辑编辑神器
  5. 基于android平台的模拟血压计实现(surfaceView的熟练使用)
  6. python进行各项统计检验_Python进行各项统计检验
  7. 把Unity包体构建到steam的后台
  8. UIP协议栈笔记·二
  9. 设计模式系列之装饰模式(Decorator Pattern)
  10. leetcode 1709. 访问日期之间最大的空档期---窗口函数lead