Android TextView文字横向自动滚动(跑马灯)

发布时间:2025-05-18 13:32

遇到行人横过马路时,应提前开启转向灯,让他们了解你的动向。 #生活技巧# #驾驶安全技巧# #正确使用转向灯#

最新推荐文章于 2025-04-09 14:09:01 发布

stevenzqzq 于 2018-04-24 13:54:02 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

TextView实现文字滚动需要以下几个要点:

1.文字长度长于可显示范围:android:singleLine=”true”
2.设置可滚到,或显示样式:android:ellipsize=”marquee”
3.TextView只有在获取焦点后才会滚动显示隐藏文字,因此需要在包中新建一个类,继承TextView。重写isFocused方法,这个方法默认行为是,如果TextView获得焦点,方法返回true,失去焦点则返回false。跑马灯效果估计也是用这个方法判断是否获得焦点,所以把它的返回值始终设置为true。

public class AlwaysMarqueeTextView extends TextView { public AlwaysMarqueeTextView(Context context) { super(context); } public AlwaysMarqueeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean isFocused() { return true; }

123456789101112131415161718

在布局XML文件中加入这么一个AlwaysMarqueeTextView

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:focusable="true" tools:context=".MainActivity"> <com.example.administrator.marqueeprojecttest.AlwaysMarqueeTextView android:layout_marginTop="45dp" android:id="@+id/AMTV1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:singleLine="true" android:text="@string/zhouenlai" /> <com.example.administrator.marqueeprojecttest.AlwaysMarqueeTextView android:layout_marginTop="45dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:singleLine="true" android:text="@string/zhouenlai2" /> </LinearLayout>

123456789101112131415161718192021222324252627

ellipsize属性
设置当文字过长时,该控件该如何显示。有如下值设置:”start”—–省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动)

marqueeRepeatLimit属性
在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。

focusable属性
自己猜测的,应该是能否获得焦点,同样focusableInTouchMode应该是滑动时能否获得焦点。

参考链接
源码下载地址:https://download.csdn.net/download/qq_26296197/10370470
https://blog.csdn.net/muyu114/article/details/6400563

网址:Android TextView文字横向自动滚动(跑马灯) https://www.yuejiaxmz.com/news/view/993237

相关内容

设置iframe显示滚动条
AE如何制作滚动数字,滚动文字风格动画教程
Android 个人理财工具一:项目概述与启动界面的实现
移动应用开发与操作系统的交互:深入理解Android和iOS
掌握时间,轻松规划——揭秘Android横向日历的实用技巧与高效生活之道
TextView属性android:ellipsize=“marquee“不生效的解决办法
Android 实例
Android天气预报应用开发实战
探索移动应用开发:从概念到实现
Android 天气APP(五)天气预报、生活指数的数据请求与渲染

随便看看