继承TextView获取焦点实现跑马灯

 1 public class MarqueeFocuseTextView extends TextView {
 2 
 3     public MarqueeFocuseTextView(Context context, AttributeSet attrs,
 4             int defStyle) {
 5         super(context, attrs, defStyle);
 6     }
 7 
 8     public MarqueeFocuseTextView(Context context, AttributeSet attrs) {
 9         super(context, attrs);
10     }
11 
12     public MarqueeFocuseTextView(Context context) {
13         super(context);
14     }
15     
16     @Override
17     @ExportedProperty(category = "focus")
18     public boolean isFocused() {
19         //始终取得焦点
20         return true;
21     }
22     
23     @Override
24     protected void onFocusChanged(boolean focused, int direction,
25             Rect previouslyFocusedRect) {
26         //写这个方法是为了去掉父类的方法
27         //super.onFocusChanged(focused, direction, previouslyFocusedRect);
28     }
29 }

关于MarqueeText类中为什么要复写onFocusChanged()方法,那是因为如果不写,在Textview 获得焦点后,再失去焦点时 字就会停止“跑”了,所以如果想让它一直跑下去就复写onFocusChanged(),并且里面什么也不做(主要是不能调用父类的方法)

 

1 <com.zzqhfuf.mobilesafefb.views.MarqueeFocuseTextView
2         android:id="@+id/tv_index_newmessage"
3         android:layout_width="match_parent"
4         android:layout_height="wrap_content"
5         android:ellipsize="marquee"
6         android:singleLine="true"
7         android:text="点过看过千万不要错过,错过悔一生啊啊啊啊啊啊啊啊啊!!!!!!!!!!!!!!!!!" 
8         android:textColor="@color/black"
9         android:textSize="15sp"/>

 

textview显示的内容一定要大于你设置的宽度,不然无法实现跑马灯的效果

当然也可以不重新创建一个类,只需在xml中的TextView写入

 

1         android:focusable="true"
2         android:focusableInTouchMode="true"

就可以实现跑马灯的效果

 

 

 

 

转载于:https://www.cnblogs.com/zzqhfuf/p/4769639.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值