android TextView实现跑马灯效果
作者:段合江 发布时间:2023-07-27 16:35:31
标签:android,TextView,跑马灯
本文实例为大家分享了android TextView跑马灯效果的具体代码,供大家参考,具体内容如下
一、要点
设置四个属性
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
直接在xml中使用
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />
注意:singleLine属性 不能换成 maxlLines
二、复杂布局
在复杂的布局中可能不会实现跑马灯效果。例如如下布局中,就只有第一个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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv1"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv1"
android:layout_marginTop="10dp"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />
</RelativeLayout>
这时候就需要自定义View,实现跑马灯效果
自定义MarQueeTextView extents TextView 重写isFocused()方法,返回true
public class MarqueeText extends TextView {
public MarqueeText(Context context) {
super(context);
}
public MarqueeText(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MarqueeText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean isFocused() {
return true;
}
}
布局中使用
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.dhj.marqueedemo.View.MarqueeText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv1"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />
<com.example.dhj.marqueedemo.View.MarqueeText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv1"
android:layout_marginTop="10dp"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />
</RelativeLayout>


猜你喜欢
- 创建一个用户类类型的集合,手动输入用户库主要是判定输入的用户名和密码是否与库中的匹配做好区别是用户名输入错误还是密码输入错误的提示。定义用户
- 算法简介迪杰斯特拉算法(Dijkstra)是由荷兰计算机科学迪家迪杰斯特拉于1959年提出的,因此又叫狄克斯特拉算法。是从一个顶点到其余各顶
- LinkedList简介LinkedList是一个使用双向链表结构实现的容器,与ArrayList一样,它能动态扩充其长度,LinkedLi
- 哈喽大家好啊,我是Hydra。Spring作为项目中不可缺少的底层框架,提供的最基础的功能就是bean的管理了。bean的注入相信大家都比较
- 一、什么是命令模式命令模式是一个高内聚的模式,其定义为:将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请 求排队或者记录
- 前言本文将提供一个redis的工具类,可以用在Spring boot以及Spring Cloud项目中,本工具类主要整合了将Redis作为N
- 如何高效地进行数组复制?如果把一个变量值复制给另外一个数组变量,那么2个变量指向托管堆上同一个引用。如果想在托管堆上创建另外的一份数组实例,
- SpringMVC域对象共享数据一、域对象1. 域对象的作用就是在一定范围内可以共享数据,通常有 3 种:request: 一次请求,多个资
- 在Android中偶尔会用到开关,Switch就是一个简单易使用的不错的控件。首先,在布局中添加上Switch控件:<Switch &
- springboot-dubbo cannot be cast to使用spring boot 集成dubbo的时候遇到问题:2018-05
- Java音频播放,因为必须依赖到本地环境,所以JAVA在音频处理方面优势不大,或者说打从Java体系开发时就没太多的考虑音频播放
- 前言复习一下spring实现IOC的源码流程准备工作:强烈建议大家从git上拉取spring源码来学习Spring源码。因为里面相较于IDE
- 本地jvm执行flink带web ui使用StreamExecutionEnvironment executionEnvironment =
- 本文实例为大家分享了Android悬浮窗菜单的具体代码,供大家参考,具体内容如下MainActivity.java代码:package si
- Interval操作符:用于创建Observable,跟TimerTask类似,用于周期性发送信息,是一个可以指定线程的TimerTask首
- 本文实例讲述了C++判断pe文件的方法。分享给大家供大家参考。具体实现方法如下:#include <afxdlgs.h>是为了使
- 一.RabbitMQ消息丢失的三种情况第一种:生产者弄丢了数据。生产者将数据发送到 RabbitMQ 的时候,可能数据就在半路给搞丢了,因为
- Author:jeffreyDate:2019-04-08一、开发环境:1、mysql - 5.72、navicat(mysql客户端管理工
- 由于项目上的需要侧滑条目展示收藏按钮,记得之前代码家有写过一个厉害的开源控件 AndroidSwipeLayout 本来准备直接拿来使用,但
- Android Fragment滑动组件ViewPager的实例详解1适配器FragmentPagerAdapter的实现对于Fragmen