Android中一直走马灯效果的TextView

XML布局:

[html]  view plain copy
  1. <TextView  
  2.         android:layout_width="100dip"  
  3.         android:layout_height="wrap_content"  
  4.         android:ellipsize="marquee"  
  5.         android:focusable="true"  
  6.         android:focusableInTouchMode="true"  
  7.         android:marqueeRepeatLimit="marquee_forever"  
  8.         android:singleLine="true"  
  9.         android:text="走马灯xml布局实现,哈哈。。。。" />  

自定义控件:

[java]  view plain copy
  1. package com.android.widget;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Rect;  
  5. import android.text.TextUtils.TruncateAt;  
  6. import android.util.AttributeSet;  
  7. import android.widget.TextView;  
  8.   
  9. /** 
  10.  *  
  11.  * 一直走马灯效果控件 
  12.  *  
  13.  * @author zuolongsnail 
  14.  */  
  15. public class CustomMarqueeTextView extends TextView {  
  16.   
  17.     public MyMarqueeTextView(Context context) {  
  18.         super(context);  
  19.         createView();  
  20.     }  
  21.   
  22.     public MyMarqueeTextView(Context context, AttributeSet attrs) {  
  23.         super(context, attrs);  
  24.         createView();  
  25.     }  
  26.   
  27.     public MyMarqueeTextView(Context context, AttributeSet attrs, int defStyle) {  
  28.         super(context, attrs, defStyle);  
  29.         createView();  
  30.     }  
  31.   
  32.     private void createView() {  
  33.         setEllipsize(TruncateAt.MARQUEE);  
  34.         setMarqueeRepeatLimit(-1);  
  35.         setFocusableInTouchMode(true);  
  36.     }  
  37.   
  38.     @Override  
  39.     protected void onFocusChanged(boolean focused, int direction,  
  40.             Rect previouslyFocusedRect) {  
  41.         if (focused) {  
  42.             super.onFocusChanged(focused, direction, previouslyFocusedRect);  
  43.         }  
  44.     }  
  45.   
  46.     @Override  
  47.     public void onWindowFocusChanged(boolean focused) {  
  48.         if (focused) {  
  49.             super.onWindowFocusChanged(focused);  
  50.         }  
  51.     }  
  52.   
  53.     @Override  
  54.     public boolean isFocused() {  
  55.         return true;  
  56.     }  
  57.   
  58. }  

在xml中的使用方法:

[html]  view plain copy
  1. <com.android.widget.CustomMarqueeTextView  
  2.             android:id="@+id/custom_marquee_textview"  
  3.             android:layout_width="wrap_content"  
  4.             android:layout_height="wrap_content" />  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值