android实现跑马灯效果
2024-08-28 09:42:37
第一步:新建一个新项目,MarqueeTextView
首先为了观察到跑马灯效果,将要显示的文字极可能 写长。在strings.xml目录里面将
<string name="hello_world">hello_world</string>
改动为
<string name="hello_world">我的代码非常长,真的非常长,不行你看看。实际上是骗你的,逗比,hiahia~~~~</string>
默认情况下。显示文字会自己主动换行!为了实现跑马灯效果。首先要阻止其自己主动换行。通过使用singleLine属性来实现!
android:singleLine=”true”
假设其目的不过实现单行文字的跑马灯效果。能够只再通过三个语句来实现!
android有个ellipsize属性。
android:ellipsize=”marquee”
android:focusable=”true”
android:focusableInTouchMode=”true”
通过上述代码能够实现单行文字的跑马灯效果。可是假设要实现多行文字的跑马灯效果,将上述代码反复书写,不能实现预想功能!
由于在上述的focusable属性里面已经将焦点定义到第一行上!兴许的得不到focus!
能够通过新建一个class类来实现!
在src目录里面新建一个类命名为MarqueeText
package com.example.marqueetextviewdemo1;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class MarqueeText extends TextView{
public MarqueeText(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MarqueeText(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public boolean isFocused() {
return true;
}
}
然后再将main.xml文件中面的TextView所有改动为 包名+.+类名。
此次改动是为了将所有以这样的方式定义的,都能获得focus。然后实现跑马灯的效果!
显示的效果例如以下:
最新文章
- 判断字符串的首字母 ---------startsWith
- css教程
- 设计模式学习之迭代器模式(Iterator,行为型模式)(17)
- python : HTML+CSS (左侧菜单)
- springmvc学习第二天
- C++实现按绩点排名
- 【转】代码编辑器(二)-SynEdit
- XSD - <;schema>; 元素
- IOS 学习笔记 2015-03-24 OC-API-不可变字符串
- 远程连接MySQL 不允许
- XStream 用法汇总
- Java: server/client 心跳机制实现 示例
- 解决axios在ie浏览器下提示promise未定义的问题
- 2019年5月1日起安卓应用应基于API 26开发,那么API等级是啥?
- CSS 标签选择器
- 在linux系统安装tomcat后,bin文件下startup.sh启动不
- unity, 在image effect shader中用_CameraDepthTexture重建世界坐标
- CF983B XOR-pyramid
- 自己实现一个一致性 Hash 算法
- nodejs备忘总结(一) -- 基础入门
热门文章
- 诡异的malloc函数和%S的输出
- C++ 代码静态分析工具cppcheck【转】
- linux内核之进程的基本概念(进程,进程组,会话关系)
- Nginx没有启动文件、nginx服务不支持chkconfig、nginx无法自启
- ANT---调用外部命令的一些坑
- MySql_ procedure
- Java定义接口变量为接收类型有什么好处(面向接口编程)
- JS中原型链中的prototype与_proto_的个人理解与详细总结(**************************************************************)
- django 设置局域网内访问项目
- odoo 有哪些文档资源