TextView跑马灯效果的两种实现方法
来源:互联网 发布:建筑效果图软件 编辑:程序博客网 时间:2024/04/28 03:14
网上还有其他的方法来实现,这里我只简单介绍两种我自己用的方法。由于跑马灯效果必须要在获得焦点的情况下才能正确显示,所以下面的方法都是围绕如何获得焦点来进行的。
第一种:通过xml布局文件的方式来实现,下面是具体代码
<TextView android:id="@+id/tv_ad" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true" android:text="测试滚动文字123456789测试滚动文字123456789测试滚动文字123456789测试滚动文字123456789"/>
主要是这三个参数android:ellipsize=”marquee” android:focusable=”true” android:focusableInTouchMode=”true”,另外需要设置单行显示才可以。
第二种:通过自定义控件的方式重写TextView的方法,来强制设置焦点
创建一个类,继承并实现TextView的默认构造方法,然后重写父类的isFocused方法,强制输出true,代码如下
public class FocusedTextView extends TextView { /** * 这个构造方法是在用代码new的时候调用 */ public FocusedTextView(Context context) { super(context); } /** * 这个构造方法是有属性的时候调用 */ public FocusedTextView(Context context, AttributeSet attrs) { super(context, attrs); } /** * 这个构造方法是有style样式的时候调用 */ public FocusedTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } /** * 强制设置焦点 */ @Override public boolean isFocused() { return true; }}
然后在布局文件当中使用这个自定义控件
<com.xxxlu.top.view.FocusedTextView android:id="@+id/tv_ad" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:layout_marginTop="5dp" android:ellipsize="marquee" android:text="测试滚动文字123456789测试滚动文字123456789测试滚动文字123456789测试滚动文字123456789"/>
至此就可以正确显示了。
上面两种方法各有自己的特点,可以根据自己的项目需要灵活使用。
0 0
- TextView跑马灯效果的两种实现方法
- Android TextView实现跑马灯效果的两种方式
- TextView跑马灯的两种实现
- android TextView不用获得焦点实现跑马灯效果 两种方法
- Android TestView实现跑马灯效果的两种方法
- 【转】TextView跑马灯效果实现方法
- 跑马灯效果的两种实现
- XML 实现 TextView的跑马灯效果
- Android TextView 跑马灯效果的实现
- TextView实现跑马灯的效果
- TextView实现跑马灯的效果
- 实现文字的跑马灯效果,Textview
- Android实现跑马灯效果的TextView
- 使用TextView实现跑马灯的效果
- 使用TextView实现跑马灯的效果
- TextView实现跑马灯的效果
- TextView跑马灯效果的实现
- TextView实现跑马灯的效果
- FastDFS压力测试
- SAS 9.4 的sid问题解决方案汇总(头疼...)
- unity编码日常(三)优化问题
- 树状数组详解
- android开发 华为手机锁屏存活,开机自启动,一键不清理,手动杀死自动起来
- TextView跑马灯效果的两种实现方法
- 数组指针和指针数组的区别
- iOS APP之间的通讯传值
- javascript单线程
- PHP 文件中存在多个mysql连接的问题
- android使用http协议连接服务器
- POJ 2348 Euclid's Game
- 归并排序及其应用 c++实现
- CSAPP阅读感受及最近状态