TextView的跑马灯效果实现
TextView的跑马灯效果实现
问题描述
当文字内容过长,但是只允许显示一行时,可以将文字显示为跑马灯效果,即文字滚动显示。
代码实现
第一种方法实现
先查询TextView控件的属性,得到以下信息:
- android:ellipsize=”marquee”
TextView采用跑马灯属性. - android:marqueeRepeatLimit=”marquee_forever”
设置重复滚动的次数,marquee_forever表示无限次.
在设置了上面两个属性之后,还需要设置两个属性,使得TextView可以获得焦点,滚动起来,不获取焦点,TextView并不会滚动。
- android:focusableInTouchMode=”true”
在Touch模式下可以获取焦点。 - android:focusable=”true”
TextView可以获取焦点。
TextView的设置属性如下:
<TextView
android:id="@+id/tv"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
android:focusable="true"
android:gravity="center"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="Hello World!相当长的内容,只能显示一行,内容太多,显示不下,所以采用跑马灯方式显示,哈哈哈哈哈。。。"
/>
这种方法实现存在一个问题,就是当其他控件获取焦点之后,TextView没有了焦点,则会停止跑马灯效果。
在TextView控件下面添加一个EditText控件,当点击EditText控件时,EditText控件将会获得焦点,TextView将会失去焦点,代码如下:
<TextView
android:id="@+id/tv"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
android:focusable="true"
android:gravity="center"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="Hello World!相当长的内容,只能显示一行,内容太多,显示不下,所以采用跑马灯方式显示,哈哈哈哈哈。。。" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="@id/tv"
/>
运行代码之后,点击EditText,上面的TextView就会停止跑马灯效果,所以如果布局上有其他控件就不适合第一种方式。
第二种方法实现
通过修改TextView的isFocus()方法,使其返回为true,可以一直获取焦点。代码如下:
public class FocusTextView extends TextView{
public FocusTextView(Context context) {
super(context);
}
public FocusTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public FocusTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean isFocused() {
return true;
}
}
FocusTextView的使用为:
<com.zhangmiao.sixproject.FocusTextView
android:id="@+id/focus_tv"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="marquee"
android:gravity="center"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:layout_below="@id/tv"
android:text="Hello World!相当长的内容,只能显示一行,内容太多,显示不下,所以采用跑马灯方式显示,哈哈哈哈哈。。。"
/>
这种方式就不会有第一种方式的问题。
总结
第一种方式实现方便快捷,但是存在被其他控件夺取焦点之后效果消失的问题。
第二种方式需要实现一个类去继承TextView,重写isFocus()方法,没有第一种方式的问题。
推荐第二种方式。
联系方式:1006299425@qq.com,有问题欢迎大家指出。
TextView的跑马灯效果实现的更多相关文章
- Android界面(1) 使用TextView实现跑马灯效果
方法一:(只能实现单个TextView的跑马灯效果)在TextView添加以下控件 android:singleLine="true"只能单行,超出的文字显示为"...& ...
- ListView 中的TextView实现跑马灯效果
案例:怎么样在一个ListView中含有TextView的item中实现字母滚动呢.这个在一些特定的场合经常用得到.如下图,当焦点位于某个item的时候其内容就自动滚动显示 要实现这样的效果,废话不多 ...
- TextView的跑马灯效果(AS开发实战第二章学习笔记)
TextView的跑马灯效果跑马灯用到的属性与方法说明singleLine 指定文本是否单行显示ellipsize 指定文本超出范围后的省略方式focusable 指定是否获得焦点,跑马灯效果要求设置 ...
- Android使用TextView实现跑马灯效果(自定义控件)
对于一个长的TetxView 折行显示是一个很好的办法,另一种方法就是跑马灯显示(单行滚动) 1.折行显示的长TextView <LinearLayout xmlns:android=" ...
- TextView 实现跑马灯效果
在String.xml中添加: <string name="txt">跑马灯效果,我跑啊跑</string>在layout/mian.xml中添加TextV ...
- Android 使用TextView实现跑马灯效果
前言 我们在开发中经常会遇到一个小问题.比如下面一个小例子: 这个文字太长,单行中导致无法全部显示出来,这就是今天要实现的功能. 当然,百度中也有很多这种解决方案. 其中有一种,例如: <Tex ...
- andriod给ListView中的TextView增加跑马灯效果
正常情况下跑马灯效果只需要在TextView中添加android:ellipsize="marquee" android:singleLine="true" a ...
- 安卓之文本视图TextView及跑马灯效果
一.基本属性和设置方法 二.跑马灯用到的属性与方法说明 三.省略方式的取值说明 四.跑马灯效果案例代码 (1)布局xml文件 <?xml version="1.0" en ...
- Android开发之TextView实现跑马灯效果
TextView及其子类,当字符内容太长显示不下时可以省略号代替未显示的字符:省略号可以在显示区域的起始,中间,结束位置,或者以跑马灯的方式显示文字(textview的状态为被选中). 其实现只需在x ...
随机推荐
- 监控服务器配置(五)-----Redis_exporter安装配置
1.下载redis_exporter安装包(linux版)到 /opt/minitor/redis_exporter . 下载地址:https://download.csdn.net/download ...
- bhttpd
以前产品应用是用串口做控制台,写了一个带简单命令历史和命令补全功能的控制台Shell,用作程序的调试,包括查看系统状态和调试修改设定等等.确实非常好用,对很多现场简单问题的快速定位起到了很好的作用.系 ...
- java程序员经常使用的Intellij Idea插件
大概从去年年初开始慢慢抛弃习惯多年的eclipse,开始使用Intellij Idea,以下是我使用过的一些Intellij Idea插件: 1.lombok https://plugins.jetb ...
- apache-tomcat 部分中文.html .jsp 连接 404问题
修改文件到 自己的安装目录:\apache-tomcat-7.0.79\conf 添加 Connector URIEncoding="utf-8" <Connector ...
- 解决Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 and 6 Web modules
1.在eclipse的workspace里面找到该项目.settings文件夹 2.编辑org.eclipse.wst.common.project.facet.core.xml文件 <?xml ...
- 服务器上部署Struts2的web项目报struts-default.xml:131:154的解决方法
背景: 用学生特价买了阿里云的服务器一年,为了练手,开始把毕业设计项目部署到该服务器上去. 项目使用的技术:Struts2 服务器上用tomcat启动该项目. 问题: 在楼主本地启动tomcat成功, ...
- faster-rcnn 笔记
2019-02-18,15点00 ''' 下面是别人写的原始的笔记,我在上面自己补充了一些. ''' #https://www.cnblogs.com/the-home-of-123/p/974796 ...
- 解决js数组循环删除出错
for(var i=0,flag=true,len=arr.length;i<len;flag ? i++ : i){ if( arr[i]&&arr[i].status==0 ...
- Flask cookie
一 什么是cookie 什么是cookie?如果单单从数据结构的角度来说,它可以被理解成用来保存数据的一个dictionary,由一组组键值对组成.如果从作用上来说,我们知道Http协议 ...
- Maven学习 六 pom.xml文件
java jar包的搜索网址:http://mvnrepository.com/ pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需 ...