Android第三天
来源:互联网 发布:网络显卡联机 编辑:程序博客网 时间:2024/06/05 15:59
TextView 实现跑马灯显示效果
一、对于一个TextView只需要设置三个条件
1、android:ellipsize=”marquee”
2、TextView必须单行显示,即内容必须超出TextView大小
3、TextView要获得焦点才能滚动
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true" android:marqueeRepeatLimit="marquee_forever" android:text="跑马灯,跑马灯,跑马灯,跑马灯,跑马灯,跑马灯" />
二、但当有多个控件同时需要实现该效果的时候上面的方法就不管用了
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true" android:marqueeRepeatLimit="marquee_forever" android:text="跑马灯,跑马灯,跑马灯,跑马灯,跑马灯,跑马灯" /><TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginTop="10dp" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true" android:marqueeRepeatLimit="marquee_forever" android:text="跑马灯,跑马灯,跑马灯,跑马灯,跑马灯,跑马灯" />
这样做只有第一个TextView有效果,因为第二个TextView它获取不到焦点了。
所以就需要自定义一个MarqueeText类,继承自TextView,并且重写isFocuse()方法,让它永远返回true,便实现了两个MarqueeText都被Focused,用MarqueeText来完成TextView的功能,这样跑马灯效果就能一直的跑起来了。
//class MarqueeTextpublic class MarqueeText extendsandroid.support.v7.widget.AppCompatTextView { public MarqueeText(Context context) { super(context); } public MarqueeText(Context context, AttributeSet attrs) { super(context, attrs); } public MarqueeText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean isFocused() { return true; }}//XML<com.example.chenjipayne.a003.MarqueeText//MarqueeText前面需要加包名 android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true" android:marqueeRepeatLimit="marquee_forever" android:text="跑马灯,跑马灯,跑马灯,跑马灯,跑马灯,跑马灯" /><com.example.chenjipayne.a003.MarqueeText android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginTop="10dp" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true" android:marqueeRepeatLimit="marquee_forever" android:text="跑马灯,跑马灯,跑马灯,跑马灯,跑马灯,跑马灯" />
阅读全文
1 0
- android第三天总结
- 第三天 android体系结构
- Android基础第三天
- Android第三天
- android自学第三天
- Android基础第三天
- android第三天
- Android开发第三天
- Android 第三天布局
- Android集训第三天
- Android第三天
- Android第三天
- Android第三天
- Android学习笔记: 第三天
- worklight第三天,集成android
- Android自学笔记 第三天
- Android 第三天(上午)
- Android 第三天(下午)
- Java中不同进制的表现形式以及int,char,String之间的转换
- 集群/分布式环境下5种session处理策略
- js模块化概览
- 1020. 月饼 (25)
- 工作一年心得
- Android第三天
- Hadoop集群环境的搭配
- linux常用命令(2)
- 数据结构--插入排序(InsertSort)思想与实现
- Codeforces 844 B Rectangles
- 错误:无法将文件XXXX.dll”复制到XXXX.dll。对路径xxx.dll”的访问被拒绝。dll”的访问被拒绝。
- MFC 获取MAC-IP-NetworkName
- 分布式事务,高并发下分布式事务的解决方案
- 自定义view——wrap_context