设为首页 加入收藏

TOP

Android TextView实现跑马灯效果
2014-11-23 23:16:59 来源: 作者: 【 】 浏览:7
Tags:Android TextView 实现 马灯 效果

研究下了让TextView一行显示数据,多的数据用省略号来表示,实现了,然后又继续研究看怎么样能够使TextView实现跑马灯效果,这样用户可以完整的看到所有的数据。


效果图如下



android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"


但是这样子有一个缺点,就是这种状态的跑马灯只能在TextView处于焦点状态的时候,它才会滚动,对于实际的开发应用中很不实用,为了是跑马灯无论在什么情况下都能跑起来,这里需要自定义一个TextView,它继承TextView,并且重写isFocuse()方法,让它永远返回true,这样跑马灯效果就能一直的跑起来了。


public class MarqueeTextView extends TextView {


public MarqueeTextView(Context context) {
super(context);
}

public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}


public MarqueeTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}

@Override
public boolean isFocused() {
return true;
}


}


在xml中引用


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/marquee_text1" />


android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/marquee_text2" />


这样就可以显示了



效果图见上。


-------------------------------分割线-------------------------------


再来一例:


1.TextView一行显示数据,多的数据用省略号来表示


android:layout_width="200dip"
android:id="@+id/textView1"
android:text="" android:singleLine="true"
android:ellipsize="end"
>


2.TextView实现跑马灯效果


android:layout_width="200dip"
android:id="@+id/textView1"
android:text="" android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusableInTouchMode="true"
android:focusable="true">


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android异步操作----AsyncTask类 下一篇Android实现分享到微博、人人网等..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: