Android实现多个TextView同时显示跑马灯效果
来源:互联网 发布:淘宝店经营地址怎么写 编辑:程序博客网 时间:2024/06/02 10:06
最近被一个页面弄得很蛋疼,这个页面比较小,需要显示较长的文案时无法显示完全,于是很自然地想到了TextView中的marquee —— 跑马灯效果,可是Android执行跑马灯效果需要控件获取焦点,当某一控件requestFocus()时,会将其他控件的焦点抢去,这就导致了同一时间只能有一个控件获取焦点。那么,如果让多个控件同时获取并持有焦点呢?或者说,“欺骗”Android系统,让它以为多个控件都持有焦点,即每个控件都在焦点状态。
通过上述分析,问题转化为如何让多个控件同时处于焦点状态,这就需要重写TextView的部分方法,达到“欺骗”Android系统的目的,这样每个控件requestFocus()之后,均让自己处于焦点状态,并且不可被剥夺焦点,就可以达到多个控件同时“持有”焦点了,代码如下:
public class MarqueeTextView extends TextView{
public MarqueeTextView(Context context)
{
this(context, null);
}
public MarqueeTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
setFocusable(true);
setFocusableInTouchMode(true);
setSingleLine();
setEllipsize(TextUtils.TruncateAt.MARQUEE);
setMarqueeRepeatLimit(-1);
}
public MarqueeTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
setFocusable(true);
setFocusableInTouchMode(true);
setSingleLine();
setEllipsize(TextUtils.TruncateAt.MARQUEE);
setMarqueeRepeatLimit(-1);
}
@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:focusable="true"、android:focusableInTouchMode="true"、android:singleLine="true"、android:ellipsize="marquee"
在代码中均已设置,故引用此控件时,无需添加上述4个属性。
转摘自:http://www.cnblogs.com/zealotrouge/archive/2013/04/19/3031373.html
0 1
- Android实现多个TextView同时显示跑马灯效果
- Android实现多个TextView同时显示跑马灯效果
- Android实现多个TextView同时显示跑马灯效果
- Android实现多个TextView同时显示跑马灯效果
- Android-多个TextView实现跑马灯效果
- 多个TextView实现跑马灯效果
- 多个TextView跑马灯效果实现
- Android实现多个跑马灯效果,多个文本框TextView的跑马灯
- Android实现多个跑马灯效果,多个文本框TextView的跑马灯
- android TextView显示跑马灯的效果
- Android TextView跑马灯效果实现
- android textview跑马灯效果实现
- Android TextView实现跑马灯效果
- android TextView实现跑马灯效果
- android TextView实现跑马灯效果
- Android开发 TextView实现跑马灯效果
- android用TextView实现跑马灯效果
- Android TextView实现跑马灯效果
- IOS中关于色值的问题
- 浙大ZOJ 1004 Anagrams by Stack问题解决
- php解析gzip
- LeetCode | Trapping Rain Water
- MySql 数据表不识别
- Android实现多个TextView同时显示跑马灯效果
- jacob操作简介
- Android 获取assets的绝对路径
- css 四种定位方式
- Oracle插入数据并获取自增长序列ID方法
- Android4.0中蓝牙适配器state machine(状态机)的分析
- AdaBoost原理,算法实现
- ORACLE显示中文为乱码_redhat_oracle11g适用
- 总结Java解惑