一、前言介绍
在前一篇文章中( android Textview 使用之一:伸缩效果 ),我们讲解到Textview 伸缩效果。当文本内容比较多,多行文本展示不全的时候,为了不影响布局美观,提高用户体验,我们会使用到Textview 伸缩效果。当文本内容不多,无需多行文本展示,单行文本展示不全时,此时选择Textview 走马灯效果更好。
实现:下面简单介绍Textview走马灯实现过程,有几点需要说明:
1、确保Textview获得焦点;
2、只有需要展示的文本不能够在Textview完全展示时,才可能出现走马灯效果。
二、实例截图
先看下示例代码效果,截图如下:
三、代码讲解
XML文件布局如下:
<span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!-- 实现走马灯效果 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_info"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
/>
</RelativeLayout></span>
XML文件说明:
android:singleLine="true" 设置为单行,默认为false
android:focusable="true" touch模式下,设置获取焦点,默认为false
android:focusableInTouchMode="true" 设置获取焦点,默认为false
android:ellipsize="marquee" 设置为滚动
android:marqueeRepeatLimit="marquee_forever" 设置重复次数
以上几点必须设置,如果以上几点均已设置,仍没有出现走马灯效果。请参考上文几点说明做检查。
另外关于 android:focusable 和 android:focusableInTouchMode 更详细的理解,可参考:http://www.cnblogs.com/frydsh/archive/2012/10/15/2724909.html
四、示例下载
下面为Demo示例下载链接,考虑大家积分问题,我把积分设置为0,大家放心下载。http://download.youkuaiyun.com/detail/improveyourself/7488221
为了大家了解更多,网上找了资源供大家扩展,链接地址为:http://www.cnblogs.com/Gaojiecai/p/3142783.html 在此感谢这位同行的奉献。