经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android 使用TextView实现跑马灯效果
来源:cnblogs  作者:carry莫奈  时间:2019/1/25 9:06:15  对本文有异议

前言

我们在开发中经常会遇到一个小问题。比如下面一个小例子:

 

这个文字太长,单行中导致无法全部显示出来,这就是今天要实现的功能。 当然,百度中也有很多这种解决方案。

其中有一种,例如:

  1. <TextView
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:singleLine="true"
  5. android:ellipsize="marquee"
  6. android:focusable="true"
  7. android:focusableInTouchMode="true"
  8. android:text="@string/hello_world" />

android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"

xml中添加这3行 就能实现效果了。

 

这种方法确实可以实现。

事实上开发过程中,布局是非常复杂和多变的,并不是我们一个TextView就能解决所有的布局和要求。

例如,现在用两个TextView 

  1. 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. 2 xmlns:tools="http://schemas.android.com/tools"
  3. 3 android:layout_width="match_parent"
  4. 4 android:layout_height="match_parent"
  5. 5 android:paddingBottom="@dimen/activity_vertical_margin"
  6. 6 android:paddingLeft="@dimen/activity_horizontal_margin"
  7. 7 android:paddingRight="@dimen/activity_horizontal_margin"
  8. 8 android:paddingTop="@dimen/activity_vertical_margin"
  9. 9 tools:context=".MainActivity" >
  10. 10
  11. 11 <TextView
  12. 12 android:id="@+id/textview1"
  13. 13 android:layout_width="wrap_content"
  14. 14 android:layout_height="wrap_content"
  15. 15 android:ellipsize="marquee"
  16. 16 android:focusable="true"
  17. 17 android:focusableInTouchMode="true"
  18. 18 android:singleLine="true"
  19. 19 android:text="@string/hello_world" />
  20. 20
  21. 21 <TextView
  22. 22 android:layout_width="wrap_content"
  23. 23 android:layout_height="wrap_content"
  24. 24 android:layout_below="@id/textview1"
  25. 25 android:ellipsize="marquee"
  26. 26 android:layout_marginTop="20dp"
  27. 27 android:focusable="true"
  28. 28 android:focusableInTouchMode="true"
  29. 29 android:singleLine="true"
  30. 30 android:text="@string/hello_world" />
  31. 31
  32. 32 </RelativeLayout>

这个简单的功能就满足不了了。

第一个跑马灯效果没问题,第二个就没实现了,平常开发中,两个TextView都解决不了,平常开发中更解决不了了。

这就是今天我所要讲的内容。

首先,我们先建一个类,继承TextView这个类。

  1. 1 package com.example.marqueetextview;
  2. 2
  3. 3 import android.content.Context;
  4. 4 import android.util.AttributeSet;
  5. 5 import android.view.ViewDebug.ExportedProperty;
  6. 6 import android.widget.TextView;
  7. 7
  8. 8 public class MarqueeText extends TextView{
  9. 9
  10. 10 public MarqueeText(Context context) {
  11. 11 super(context);
  12. 12 }
  13. 13
  14. 14 public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
  15. 15 super(context, attrs, defStyle);
  16. 16 }
  17. 17
  18. 18 public MarqueeText(Context context, AttributeSet attrs) {
  19. 19 super(context, attrs);
  20. 20 }
  21. 21 @Override
  22. 22 @ExportedProperty(category = "focus")
  23. 23 public boolean isFocused() {
  24. 24 return true;
  25. 25 }
  26. 26 }

这个时候实现TextView中的一个方法,isFocused(), 返回改成return true。

 然后进Xml中 把TextView修改成我们自定义的这个控件。

 

  1. 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. 2 xmlns:tools="http://schemas.android.com/tools"
  3. 3 android:layout_width="match_parent"
  4. 4 android:layout_height="match_parent"
  5. 5 android:paddingBottom="@dimen/activity_vertical_margin"
  6. 6 android:paddingLeft="@dimen/activity_horizontal_margin"
  7. 7 android:paddingRight="@dimen/activity_horizontal_margin"
  8. 8 android:paddingTop="@dimen/activity_vertical_margin"
  9. 9 tools:context=".MainActivity" >
  10. 10
  11. 11 <com.example.marqueetextview.MarqueeText
  12. 12 android:id="@+id/textview1"
  13. 13 android:layout_width="wrap_content"
  14. 14 android:layout_height="wrap_content"
  15. 15 android:ellipsize="marquee"
  16. 16 android:focusable="true"
  17. 17 android:focusableInTouchMode="true"
  18. 18 android:singleLine="true"
  19. 19 android:text="@string/hello_world" />
  20. 20
  21. 21 <com.example.marqueetextview.MarqueeText
  22. 22 android:layout_width="wrap_content"
  23. 23 android:layout_height="wrap_content"
  24. 24 android:layout_below="@id/textview1"
  25. 25 android:layout_marginTop="20dp"
  26. 26 android:ellipsize="marquee"
  27. 27 android:focusable="true"
  28. 28 android:focusableInTouchMode="true"
  29. 29 android:singleLine="true"
  30. 30 android:text="@string/hello_world" />
  31. 31
  32. 32 </RelativeLayout>

然后我们再看一下效果。

已经全部实现成功了。

那这到底是为什么呢? 奥秘就在我们重载的isFocused()这个函数. return true全部强制Focused.都有焦点了,就都能实现了

如果没设置,焦点都第一个,第二个就无法实现。

这个是我写的Demo:https://pan.baidu.com/s/1M1TghCh_R3kFnReM4li1VQ

 

原文链接:http://www.cnblogs.com/MrChen-/p/10310728.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号