TextView实现跑马灯效果
网上有很多跑马灯的介绍,有很多跑马灯的代码。或许我的不是最好的,但是应该很容易明白的。
我们先来介绍一个跑马灯的代码
<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实现跑马灯效果的更多相关文章
- Android界面(1) 使用TextView实现跑马灯效果
方法一:(只能实现单个TextView的跑马灯效果)在TextView添加以下控件 android:singleLine="true"只能单行,超出的文字显示为"...& ...
- TextView的跑马灯效果实现
TextView的跑马灯效果实现 问题描述 当文字内容过长,但是只允许显示一行时,可以将文字显示为跑马灯效果,即文字滚动显示. 代码实现 第一种方法实现 先查询TextView控件的属性,得到以下信息 ...
- 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 ...
随机推荐
- My97datepicker设置后一个日期大于前一个日期
<@e.text label="开始时间" name="mtpiStratTime" required="true" class=&q ...
- iOS UICollectionView基础
转载自:http://www.cnblogs.com/wayne23/p/4013522.html 初始化部分: UICollectionViewFlowLayout *flowLayout= [[ ...
- IIS连接数
IIS连接数,也叫IIS并发数(Current Connections),是指同一时间内,有多少个对服务器的请求 一般情况下一个浏览器会占用2个IIS连接 同一个浏览器(IE.Firefox等)窗口中 ...
- Xcode repository host is unreachable
遇到这个错误,首先不要急.按照如下方法即可(如果你的svn地址没有问题的话): url要使用域名,所以映射下 1. 修改host:在应用程序里面打开终端(terminal),输入 sudo vi /e ...
- [Java][Android][Process] 暴力的服务能够解决一切,暴力的方式运行命令行语句
不管是在Java或者Android中运行命令行语句殊途同归都是创建一个子进程运行调用可运行文件运行命令.类似于Windows中的CMD一样. 此时你有两种方式运行:ProcessBuilder与Run ...
- 会话跟踪session cookie
会话跟踪 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在 ...
- C#_delegate - 用委托实现事件,Display和Log类都使用Clock对象
//public event SecondChangeHandler OnSecondChange; 若将委托加上event,则视作是事件,不是委托,外围就不能直接对OnSecondChange传值 ...
- android平台获取手机IMSI,IMEI ,序列号,和 手机号的方法
1)获取运营商sim卡imsi号, String IMSI =android.os.SystemProperties.get( android.telephony.TelephonyPropertie ...
- 详解 Objective-C 中的 Runtime
公司项目用到一个三方开源库,里面有个bug,不能改动源码,我想来想去,只能通过runtime这个万能的手段来解决.但是runtime 并不怎么会用,怎么办,马上学习呗.说到runtime,它是Obje ...
- 关于"干货集中营"的一个开源App
中秋佳节,玩了一天,撸了两天代码,搞出这么个东东,共享出来,小伙伴们如果有兴趣,欢迎添砖加瓦. 数据接口为干货集中营的数据,接口地址:http://gank.io/api 使用到的技术清单如下: 1. ...