网上有很多跑马灯的介绍,有很多跑马灯的代码。或许我的不是最好的,但是应该很容易明白的。

我们先来介绍一个跑马灯的代码

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/wisdom_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你必须非常努力,才能看起来毫不费劲!-Moon同学的励志语言"
android:textSize="24sp"
android:singleLine="true"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
android:focusable="true"
/> <!--
android:ellipsize="start" 省略号在开头
android:ellipsize="middle" 省略号在中间
android:ellipsize="end" 省略号在结尾
android:ellipsize="marquee" 跑马灯显示
-->
<!--
android:singleLine="true" 内容只能显示在一行
android:focusableInTouchMode="true" 通过touch来获得focus
android:focusable="true" 是否可以获取焦点
-->
</LinearLayout>

当然如果是一个跑马灯的话,那么这个就完全可以了,但是在以后的开发中,布局会很复杂的,如果出现两个以上的跑马灯的话,那么重复上面的代码,那么是实现不了的,那么两个以上的应该要怎么做呢?

layout布局的代码如下

 <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" > <com.shxt.xkl.MaequeeText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="你必须非常努力,才能看起来毫不费劲,你必须非常努力,才能看起来毫不费劲." /> <com.shxt.xkl.MaequeeText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="你必须非常努力,才能看起来毫不费劲,你必须非常努力,才能看起来毫不费劲." /> </LinearLayout>

在新建一个TextView的子类

 public class MaequeeText extends TextView {

     public MaequeeText(Context context) {
super(context);
} // 重写所有的构造函数 Source==>Generate Constructors from Superclass
public MaequeeText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} public MaequeeText(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
public boolean isFocused() {
return true;
// 自定义设置让focusable为true
// 这个方法相当于在layout中
// android:focusable="true"
// android:focusableInTouchMode="true"
}
}

以上代码就能解决了今后多个跑马灯的问题了,希望对大家有帮助!

                                                                        ~Moon童鞋

TextView实现跑马灯效果的更多相关文章

  1. Android界面(1) 使用TextView实现跑马灯效果

    方法一:(只能实现单个TextView的跑马灯效果)在TextView添加以下控件 android:singleLine="true"只能单行,超出的文字显示为"...& ...

  2. TextView的跑马灯效果实现

    TextView的跑马灯效果实现 问题描述 当文字内容过长,但是只允许显示一行时,可以将文字显示为跑马灯效果,即文字滚动显示. 代码实现 第一种方法实现 先查询TextView控件的属性,得到以下信息 ...

  3. ListView 中的TextView实现跑马灯效果

    案例:怎么样在一个ListView中含有TextView的item中实现字母滚动呢.这个在一些特定的场合经常用得到.如下图,当焦点位于某个item的时候其内容就自动滚动显示 要实现这样的效果,废话不多 ...

  4. TextView的跑马灯效果(AS开发实战第二章学习笔记)

    TextView的跑马灯效果跑马灯用到的属性与方法说明singleLine 指定文本是否单行显示ellipsize 指定文本超出范围后的省略方式focusable 指定是否获得焦点,跑马灯效果要求设置 ...

  5. Android使用TextView实现跑马灯效果(自定义控件)

    对于一个长的TetxView 折行显示是一个很好的办法,另一种方法就是跑马灯显示(单行滚动) 1.折行显示的长TextView <LinearLayout xmlns:android=" ...

  6. TextView 实现跑马灯效果

    在String.xml中添加: <string name="txt">跑马灯效果,我跑啊跑</string>在layout/mian.xml中添加TextV ...

  7. Android 使用TextView实现跑马灯效果

    前言 我们在开发中经常会遇到一个小问题.比如下面一个小例子: 这个文字太长,单行中导致无法全部显示出来,这就是今天要实现的功能. 当然,百度中也有很多这种解决方案. 其中有一种,例如: <Tex ...

  8. andriod给ListView中的TextView增加跑马灯效果

    正常情况下跑马灯效果只需要在TextView中添加android:ellipsize="marquee" android:singleLine="true" a ...

  9. 安卓之文本视图TextView及跑马灯效果

    一.基本属性和设置方法 二.跑马灯用到的属性与方法说明 三.省略方式的取值说明 四.跑马灯效果案例代码   (1)布局xml文件 <?xml version="1.0" en ...

  10. Android开发之TextView实现跑马灯效果

    TextView及其子类,当字符内容太长显示不下时可以省略号代替未显示的字符:省略号可以在显示区域的起始,中间,结束位置,或者以跑马灯的方式显示文字(textview的状态为被选中). 其实现只需在x ...

随机推荐

  1. 终端神器 iterm

    1.简介 mac自带的终端terminal算蛮好用的, 但相比另一款优秀的终端软件iterm,iterm这款神器不逊于mac自带的终端.它支持了很多快捷键, 深受键盘党的喜爱. 2.下载 http:/ ...

  2. MySQL主从复制技术(纯干货)

    1.复制配置     主机一定要开启二进制日志(这里建议配置RBR)     每个主机和每个从机一定要配置一个位移的id,即server-id     每个从机配置一定要包含主机名称,日志名称,和位置 ...

  3. ThinkPHP 获取配置文件中的值

    C('SPECIAL_USER'):获取配置文件中的值 存入数组

  4. Codeforces Round #325 (Div. 2) D. Phillip and Trains BFS

    D. Phillip and Trains Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586/ ...

  5. hdu 5120 Intersection 圆环面积交

    Intersection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5 ...

  6. 网络子系统55_ip协议分片重组_加入ipq

    //ip分片加入到正确的ipq结构 //调用路径:ip_defrag->ip_frag_queue // 处理过程: // 1.正在被释放的ipq,不处理新加入的分片(ipq正在被释放由last ...

  7. Android在MediaMuxer和MediaCodec用例 - audio+video

    在Android多媒体类,MediaMuxer和MediaCodec这是一个相对年轻,他们是JB 4.1和JB 4.3据介绍. 前者被用来产生一个混合的音频和视频的多媒体文件.的缺点是,现在可以只支持 ...

  8. el表达式跟ognl表达式的区别(转)

    EL表达式: >>单纯在jsp页面中出现,是在四个作用域中取值,page,request,session,application.>>如果在struts环境中,它除了有在上面的 ...

  9. [Effective C++ --014]在资源管理类中小心copying行为

    第一节 <背景> 条款13中讲到“资源取得的时机便是初始化时机”并由此引出“以对象管理资源”的概念.通常情况下使用std中的auto_ptr(智能指针)和tr1::shared_ptr(引 ...

  10. MySQL(16):Select-union(联合查询)

    1. Select-union(联合查询) union用于把来自许多SELECT语句的结果组合到一个结果集合中. 用法: SELECT ...UNION [ALL | DISTINCT]SELECT ...