一、前言介绍

        在前一篇文章中( Textview 使用之一:伸缩效果 ),我们讲解到Textview 伸缩效果。当文本内容比较多,多行文本展示不全的时候,为了不影响布局美观,提高用户体验,我们会使用到Textview 伸缩效果。当文本内容不多,无需多行文本展示,单行文本展示不全时,此时选择Textview 走马灯效果更好。

      实现:下面简单介绍Textview走马灯实现过程,有几点需要说明:

1、确保Textview获得焦点;

2、只有需要展示的文本不能够在Textview完全展示时,才可能出现走马灯效果。


        二、实例截图

        先看下示例代码效果,截图如下:

Textview 使用之二:走马灯效果

        三、代码讲解

XML文件布局如下:

[html] view plain copy
  1. <span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <!-- 实现走马灯效果 -->  
  8.     <TextView  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/txt_info"   
  12.         android:singleLine="true"  
  13.         android:focusable="true"  
  14.         android:focusableInTouchMode="true"  
  15.         android:ellipsize="marquee"  
  16.         android:marqueeRepeatLimit="marquee_forever"  
  17.         />  
  18.   
  19. </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

相关文章: