首先我们要实现走马灯这样一个效果,通常来说都是在TextView这个控件中来实现的,而且其中的文字一定是单行显示,如果多行显示,那走马灯效果也就失去了存在的意义。另外,在EditText中使用走马灯没有必要,也不合理,实际上对于EditText来说android:ellipsize这个属性只有对于设置在android:hint中的文字的时候是有用的,而且android:ellipsize="marquee"这个用法不能用在EditText控件上。对于在EditText用户输入的文字,android:ellipsize这个属性没有用处。关于EditText设置android:ellipsize的相关用法以后再讲,在这里也算留个标记,以防自己忘了。在TextView中实现我们的走马灯效果,需要两个属性android:singleLine="true",以及android:ellipsize="marquee",我们来看下面的代码:
    <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical" >  <TextView  android:layout_width="100dip"  android:layout_height="wrap_content"  android:layout_gravity="center"  android:text="走马灯效果的演示"   android:singleLine="true"  android:ellipsize="marquee"/>      </LinearLayout>  
运行这段代码之后,我们会发现走马灯效果并没有显示出来,显示出的文字是不动的,实际效果如下:这其中的原因在于跑马灯效果需要TextVIew获得当前的焦点(focus)。然而对于TextView这个控件来说,他的默认的Clickable,LongClickable,Focusable,FocusableInTouchMode这四个属性的值都是false,所以跑马灯效果也就不会出来了,即使你用手触摸TextView或者按下手机上的导航按键(现在的手机没这个东东了都。。。)也是无法显示跑马灯的效果的。解决这个问题我们就需要让我们的TextView得到焦点,这里主要涉及android:focusable和android:focusableInTouchMode这两个属性,简单来说把这两个属性都设置成true,那么在运行程序以后跑马灯效果就显示出来了,这里就不再贴这两行代码了。但是细细品味这两个属性之后发现其中其实还是有一些玄机的:1.。如果这两个属性设置成android:focusable="true"以及android:focusableInTouchMode="false",那么会发现程序运行之后,走马灯效果没有出现,这个时候需要用户按下手机或者模拟器上的上下导航键,才能让走马灯的效果出现,这说明android:focusable是针对于手机按键有效的,然而根据api的解释,android:focusableInTouchMode是根据屏幕触摸决定的。2。如果这两个属性设置成android:focusable="false"与android:focusableInTouchMode="true",那么无论如何走马灯都出现不了了,就算加上android:clickable="true"也不行,这说明 android:focusable="true"是android:focusableInTouchMode="true"能有效的先决条件,我推测可能是在源码实现中,android:focusableInTouchMode的逻辑是嵌套在android:focusable中的,这个有待于以后进一步的研究,路漫漫其修远兮。。。3。在把这两个属性都设置成true以后,会发现程序运行之后,走马灯效果自动就显现了出来,这说明应用在运行后,会自动地按照某种顺序(在这里应该是自上而下),寻找第一个android:focusableInTouchMode="true"这个属性有效的第一个控件,当然要使这个属性有效按照前面的讨论android:focusable="true"也必须具备。根据测试,LinearLayout的Clickable,LongClickable,Focusable,FocusableInTouchMode这四个属性默认也都是false,因此,在上面的例子中TextView就率先获得了焦点,走马灯也就走了起来了。这里我们做一下验证,首先将代码修改为:
    <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical"   android:focusable="true">  <TextView  android:layout_width="100dip"  android:layout_height="wrap_content"  android:layout_gravity="center"  android:text="走马灯效果的演示"   android:singleLine="true"  android:ellipsize="marquee"  android:focusable="true"  android:focusableInTouchMode="true"  ></TextView>     </LinearLayout>  
也就是为LinearLayout加上了android:focusable="true"然后运行应用,会发现TextView的走马灯照走不误:然后我们为LinearLayout加上android:focusableInTouchMode="true"然后再运行,会发现走马灯失效了。这里也就验证了我们总结的第三条结论。但是稍等,我们在这里又发现问题了!现在无论我们怎么点击导航按钮,又或是点击屏幕上的TextView走马灯死活都走不出来了。这是怎么回事呢?让我们理顺一下思路,按照我们前面的总结,走马灯要想有效,TextView必须要得到焦点,现在走马灯出不来,说明TextView没有得到焦点。这里有两个情况,一是导航按钮无法让TextView或得焦点,二是屏幕点击无法让TextView获得焦点。先看第一种情况,第一种情况的原因在于使用导航按钮切换焦点默认的的方式会跳过内部控件,说白了,上面例子里面的TextView在LinearLayout里面,现在LinearLayout有焦点,如果你按导航按钮上下按键,焦点只会在LinearLayout同层次的控件之间切换,不会进入到Linearlayout内部,为了验证这个结论,我们使用下面的代码:
    <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical"   >  <LinearLayout android:layout_width="fill_parent"  android:layout_height="100dip"  android:focusable="true"  android:focusableInTouchMode="true"/>  <TextView  android:layout_width="100dip"  android:layout_height="wrap_content"  android:layout_gravity="center"  android:text="走马灯效果的演示"   android:singleLine="true"  android:ellipsize="marquee"  android:focusable="true"  android:focusableInTouchMode="true"  ></TextView>     </LinearLayout> 
 然后我们运行程序,会发现开始的时候走马灯没有自动运行,因为这时候焦点在代码里面的第二个LinearLayout那里,然后我们按下导航下按键,会发现走马灯效果出来了,
但是稍等,再重新运行应用,不要按导航按键,然后我们这个时候用手指或者说模拟器里的鼠标继续点击TextView,会发现走马灯还是没有出现。这个时候我们来到了第二种情况了。这里的问题可以总结为,除了应用第一次开启的时候,应用自动寻找到android:focusableInTouchMode="true"属性有效的控件冰赋予焦点,我们要如何自行通过点击屏幕的方式使一个控件获得焦点,在这种情况之下控件想要获得焦点的流程是什么。这方面的资料我没有查到,所以只能自己做一些试验,然后总结。有盲人摸象的嫌疑,但是我认为在某种程度上是管用的,继续。首先我们将代码修改为如下所示:
   <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical"   >  <LinearLayout android:layout_width="fill_parent"  android:layout_height="100dip"  android:focusable="true"  android:focusableInTouchMode="true"  />  <TextView  android:layout_width="100dip"  android:layout_height="wrap_content"  android:layout_gravity="center"  android:text="走马灯效果的演示"   android:singleLine="true"  android:ellipsize="marquee"  android:clickable="true"  android:focusable="true"  android:focusableInTouchMode="true"  ></TextView>  </LinearLayout>  
 也就是给TextView加上了一个android:clickable="true"属性,然后运行之后发现,现在通过触摸的方式点击TextView可以让走马灯初显也就是可以让TextView获得焦点了。看起来问题解决了,但是仔细想想其中还是有一些值得思考的地方:android:clickable与android:focusableInTouchMode之间是一种什么关系?是说一个空间如果要想能获得焦点就必须可点击吗?又或者说一个空间只要可以点击就一定可以获得焦点?这二者之间是充要条件还是充分条件还是必要条件?我们来做一下验证:首先运行如下代码:
    <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical"   >  <LinearLayout android:layout_width="fill_parent"  android:layout_height="100dip"  android:clickable="true"  />  <TextView  android:layout_width="100dip"  android:layout_height="wrap_content"  android:layout_gravity="center"  android:text="走马灯效果的演示"   android:singleLine="true"  android:ellipsize="marquee"  android:clickable="true"  android:focusable="true"  android:focusableInTouchMode="true"  ></TextView>  </LinearLayout>  
运行后会发现,应用开始以后跑马灯马上出现,鼠标点击TextView上方的位置也就是第二个LinearLayout的区域,跑马灯不停止,这说明:android:clickable="true"不一定就能获得焦点,也就是一个空间能点击不一定就能获得焦点。我们来看下一段代码:<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical"   >  <LinearLayout android:layout_width="fill_parent"  android:layout_height="100dip"  android:clickable="false"  android:focusable="true"  android:focusableInTouchMode="true"  />  <TextView  android:layout_width="100dip"  android:layout_height="wrap_content"  android:layout_gravity="center"  android:text="走马灯效果的演示"   android:singleLine="true"  android:ellipsize="marquee"  android:clickable="true"  android:focusable="true"  android:focusableInTouchMode="true"  ></TextView>  </LinearLayout>  这段代码运行之后,首先代码中的第二个LinearLayout自动获得焦点,然后我们点击TextView。跑马灯出现,TextView获得焦点,然后我们点击TextView上方区域,跑马灯不停止。这说明如果一个空间能获得触摸模式焦点但却不能点击,那么在触摸模式下无论怎么触摸也还是不能获得焦点的。好的我们来看最后一段代码:<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical"   >  <LinearLayout android:layout_width="fill_parent"  android:layout_height="100dip"  android:clickable="true"  android:focusable="true"  android:focusableInTouchMode="true"  />  <TextView  android:layout_width="100dip"  android:layout_height="wrap_content"  android:layout_gravity="center"  android:text="走马灯效果的演示"   android:singleLine="true"  android:ellipsize="marquee"  android:clickable="true"  android:focusable="true"  android:focusableInTouchMode="true"  ></TextView>  </LinearLayout>  这段代码运行之后,首先走马灯不出现,代码中的第二个LinearLayout获得焦点,然后我们点击第二个TextView,走马灯出现,然后我们点击TextView上方的区域,走马灯效果消失,说明焦点转移到代码中的第二个LinearLayout。好的,总结一下也就是说,在触摸模式下android:clickable="true"是(android:focusable="true",android:focusableInTouchMode="true")能获得焦点的必要条件,就是说一个控件想要在触摸模式下获得焦点就一定要可点击,上面三个属性都要有。

ellipsize实现跑马灯总结相关推荐

  1. android:ellipsize实现跑马灯效果总结

    原文地址:http://www.cnblogs.com/Gaojiecai/archive/2013/06/18/3142783.html android:ellipsize用法如下: 在xml中 a ...

  2. android:ellipsize = marquee 跑马灯,Android-单行跑马灯(一直循环)效果

    [声明:]本文是作者(蘑菇v5)原创,版权归作者 蘑菇v5所有,侵权必究.本文首发在简书.如若转发,请注明作者和来源地址!未经授权,严禁私自转载! 前言: 提示的效果,要求一直循环. 1.跑马灯效果( ...

  3. android:ellipsize = marquee 跑马灯,TextView设置android:ellipsize=marquee不滚动

    今天发现android开发中有一个android:ellipsize=marquee,这个感觉像html中的marquee功能,可以实现滚动效果,于是测试了一下,结果不行呀,后来百度了一下解决办法,下 ...

  4. android:ellipsize = marquee 跑马灯,Android基于TextView属性android:ellipsize实现跑马灯效果的方法...

    本文实例讲述了Android基于TextView属性android:ellipsize实现跑马灯效果的方法.分享给大家供大家参考,具体如下: Android系统中TextView实现跑马灯效果,必须具 ...

  5. Android:TextView 自动滚动(跑马灯) (转)

    Android:TextView 自动滚动(跑马灯) TextView实现文字滚动需要以下几个要点: 1.文字长度长于可显示范围:android:singleLine="true" ...

  6. Android文字实现跑马灯效果——两种方法实现

    第一种方法,设置textView的属性 <com.example.glide.MarqueeViewandroid:id="@+id/marqueeView"android: ...

  7. Android TextView中划线、下划线、跑马灯的简单使用

    本人安卓初学者,小白一枚,希望以写博客的方法巩固已学的技能,讲的可能不好,望大家见谅! 不墨迹 直接上 效果图 TextView 中划线 和 下划线 xml文件中(中划线和下划线一样 有个 id 就行 ...

  8. AndroidStudio案例——跑马灯

    简单的跑马灯,只需利用TextView. [效果展示] [activity_main.xml代码] <TextViewandroid:layout_width="wrap_conten ...

  9. Android 使用 ellipsize 实现文字横向移动效果(跑马灯效果)

    实现的效果图如下 ellipsize 可以设置跑马灯效果 具体代码设置如下 <TextViewandroid:layout_width="match_parent"andro ...

最新文章

  1. 基于光照的物理模型(一)
  2. 垃圾回收器算法之引用计数器法
  3. python多边形裁剪
  4. 蚂蚁御术:我的前端十年成长之路
  5. 基于CDS view自动生成的OData服务,是如何实现read操作的
  6. codeblocks 调试
  7. android学习笔记---使用AsyncTask实现异步处理,内部使用线程加Handler
  8. Microsoft caffe(caffe-windows) cifar实例编译之model的使用
  9. (二)计算软件的安装:truegrid、ls-prepost、ls-dyna
  10. 通过UDP解析域名 DNS解析
  11. 学习笔记图片从本地复制到csdn博客出现:外链图片转存失败:解决方法
  12. Xcode各种运行报错及解决方法
  13. 织梦系统参数设置出现Token mismatch!提示的解决办法
  14. ch552开发环境配置
  15. r语言赋值为na_R语言数据框中,用0替代NA缺失值
  16. 前端诡异参数start
  17. 徐则臣新作《北上》:我们的历史写在这条河流上
  18. 利用iperf网络带宽测试工具看多线程及多核编程
  19. Teamcenter RAC 查找数据集并获取数据集中文件
  20. 用python打印沙漏_打印沙漏(20)

热门文章

  1. linux系统编程:系统函数system
  2. 嘀嘀打车、快的打车该醒醒了
  3. G - 阿牛的EOF牛肉串
  4. 驾考科目一:道路交通安全法律、法规和规章
  5. CocoaPods安装firebase的问题记录
  6. SyntaxError: Non-ASCII character ‘\xe9‘ in file dun.py on line 4
  7. 原码、反码、补码以及移码的相互转换
  8. ChatGPT原理解析以及使用方法介绍
  9. python学习 爬取中华英才网工作职位
  10. 纳米数据足球数据,足球赛事比分,体育数据api,卡塔尔世界杯