Android TextView跑马灯效果
来源:互联网 发布:国家打击淘宝刷单 编辑:程序博客网 时间:2024/04/30 11:37
所谓跑马灯效果就是当文字超过控件所能容纳的空间时,在控件内滚动的效果。
走马灯的效果主要是通过android:singleLine,android:ellipsize,android:marqueeRepeatLimit,android:focusable属性来配置的。
android:singleLine="true"android:ellipsize="marquee"android:focusableInTouchMode="true"android:focusable="true"android:marqueeRepeatLimit="marquee_forever"
- android:singleLine=true 表示使用单行文字,多行文字也就无所谓使用Marquee效果了。
- android:marqueeRepeatLimit,设置走马灯滚动的次数。
- android:ellipsize,设置了文字过长时如何切断文字,可以有none, start,middle, end, 如果使用走马灯效果则设为marquee.
- android:focusable,Android的缺省行为是在控件获得Focus时才会显示走马灯效果
显示跑马灯效果的前提条件就是你的文本内容要比显示文本的外部组件长,即外部组件无法完整的显示内部的文本内容。
因此要实现跑马灯效果有两种设置方式:
1、layout_width=”"设置为成比文本内容短的固定值,最好不要写成wrap_content或者fill_parent。
2、如果layout_width=”"设置wrap_content或者fill_parent,那么可以增加上 android:paddingLeft="15dip",android:paddingRight="15dip"使两端的距离加大而无法全部显示文本内容,但是这有一个缺陷就是在手机的屏幕变大时,距离没有变大,外部组件又可以正常显示内部文本,于是又无法显示跑马灯效果,因此建议第一种设置为佳。
另外还可以设置滚动的次数android:marqueeRepeatLimit=”";android:marqueeRepeatLimit=”marquee_forever”表示一直滚动。
当有些情况下需要是文字一直滚动以引起用户注意,这是可以使用派生TextView,重载onFocusChanged,onWindowFocusChanged,isFocused 这三个方法。
修改一下本例,添加一个ScrollAlwaysTextView类:
public class ScrollAlwaysTextView extends TextView { public ScrollAlwaysTextView(Context context) { this(context, null); } public ScrollAlwaysTextView(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.textViewStyle); } public ScrollAlwaysTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { if (focused) super.onFocusChanged(focused, direction, previouslyFocusedRect); } @Override public void onWindowFocusChanged(boolean focused) { if (focused) super.onWindowFocusChanged(focused); } @Override public boolean isFocused() { return true; }}
- Android TextView跑马灯效果
- Android TextView跑马灯效果
- Android TextView跑马灯效果
- Android TextView跑马灯效果
- [Android]TextView跑马灯效果
- Android textView 跑马灯效果
- android TextView 跑马灯效果
- Android TextView跑马灯效果
- Android TextView跑马灯效果
- Android TextView跑马灯效果
- android TextView跑马灯效果
- android TextView跑马灯效果
- Android TextView跑马灯效果
- Android TextView跑马灯效果
- android textView跑马灯效果
- Android TextView 跑马灯效果
- Android TextView跑马灯效果
- android textview 跑马灯效果
- Java 汉字转拼音的具体实现及汉字声调处理
- 人气微博榜55期 邕城人气烤鱼店
- Android实现计时与倒计时的几种方法
- sqlserver技术内幕<二> 表运算符之pivot
- BZOJ1827 [Usaco2010 Mar]gather 奶牛大集会
- Android TextView跑马灯效果
- Android之Async Task异步任务分析
- sqlserver主机名变更后的错误与处理办法
- hive的表新增字段后,新增字段不能load数据
- 关于UITableView reloadData时发生App crash的情况
- html调用摄像头拍照,并生成预览
- unity3d 4.0 破解步骤
- 验证手机号
- Java网络编程总结