android TextView实现跑马灯效果

发布时间 - 2026-01-11 02:07:45    点击率:

本文实例为大家分享了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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# android  # TextView  # 跑马灯  # Android文本视图TextView实现跑马灯效果  # android使用TextView实现跑马灯效果  # Android用过TextView实现跑马灯效果的示例  # Android自定义textview实现竖直滚动跑马灯效果  # Android用TextView实现跑马灯效果代码  # 的人  # 要做  # 都得  # 自定义  # 会有  # 第一个  # 重写  # 大家分享  # 具体内容  # 大家多多  # 这时候  # lt  # brush  # wrap_content  # layout_width  # pre  # xml  # focusableInTouchMode  # class 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Swift中switch语句区间和元组模式匹配  Win11怎样安装网易有道词典_Win11安装词典教程【步骤】  5种Android数据存储方式汇总  详解Nginx + Tomcat 反向代理 负载均衡 集群 部署指南  laravel怎么实现图片的压缩和裁剪_laravel图片压缩与裁剪方法  用v-html解决Vue.js渲染中html标签不被解析的问题  高端企业智能建站程序:SEO优化与响应式模板定制开发  香港网站服务器数量如何影响SEO优化效果?  如何在IIS服务器上快速部署高效网站?  微博html5版本怎么弄发语音微博_语音录制入口及时长限制操作【教程】  打开php文件提示内存不足_怎么调整php内存限制【解决方案】  如何获取免费开源的自助建站系统源码?  如何用5美元大硬盘VPS安全高效搭建个人网站?  如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?  微信小程序 input输入框控件详解及实例(多种示例)  如何在万网ECS上快速搭建专属网站?  php嵌入式断网后怎么恢复_php检测网络重连并恢复硬件控制【操作】  如何在万网自助建站平台快速创建网站?  Laravel如何创建自定义Facades?(详细步骤)  Laravel怎么实现验证码(Captcha)功能  广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?  实例解析Array和String方法  Laravel怎么配置自定义表前缀_Laravel数据库迁移与Eloquent表名映射【步骤】  如何在Tomcat中配置并部署网站项目?  Laravel项目结构怎么组织_大型Laravel应用的最佳目录结构实践  Laravel观察者模式如何使用_Laravel Model Observer配置  猎豹浏览器开发者工具怎么打开 猎豹浏览器F12调试工具使用【前端必备】  Laravel如何使用查询构建器?(Query Builder高级用法)  如何登录建站主机?访问步骤全解析  Laravel怎么生成二维码图片_Laravel集成Simple-QrCode扩展包与参数设置【实战】  Laravel Octane如何提升性能_使用Laravel Octane加速你的应用  做企业网站制作流程,企业网站制作基本流程有哪些?  合肥制作网站的公司有哪些,合肥聚美网络科技有限公司介绍?  微信推文制作网站有哪些,怎么做微信推文,急?  php后缀怎么变mp4格式错误_修改扩展名提示格式不对怎么办【技巧】  php静态变量怎么调试_php静态变量作用域调试技巧【解答】  学生网站制作软件,一个12岁的学生写小说,应该去什么样的网站?  如何在IIS中新建站点并配置端口与IP地址?  如何快速搭建二级域名独立网站?  Laravel怎么做缓存_Laravel Cache系统提升应用速度的策略与技巧  如何正确下载安装西数主机建站助手?  如何在Windows 2008云服务器安全搭建网站?  如何快速上传建站程序避免常见错误?  Python文件操作最佳实践_稳定性说明【指导】  Laravel如何集成Inertia.js与Vue/React?(安装配置)  php增删改查怎么学_零基础入门php数据库操作必知基础【教程】  制作电商网页,电商供应链怎么做?  如何用景安虚拟主机手机版绑定域名建站?  西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?  网站制作价目表怎么做,珍爱网婚介费用多少?