Android TextView实现跑马灯效果的方法

所属分类: 软件编程 / Android 阅读数: 821
收藏 0 赞 0 分享

本文为大家分享一个非常简单但又很常用的控件,跑马灯状态的TextView。当要显示的文本长度太长,又不想换行时用它来显示文本,一来可以完全的显示出文本,二来效果也挺酷,实现起来超级简单,所以,何乐不为。先看下效果图:

代码实现

TextView自带了跑马灯功能,只要把它的ellipsize属性设置为marquee就可以了。但有个前提,就是TextView要处于被选中状态才能有效果,看到这,我们就很自然的自定义一个控件,写出以下代码:

public class MarqueeTextView extends TextView {

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

 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() {
 // TODO Auto-generated method stub
 if(getEditableText().equals(TruncateAt.MARQUEE)){
  return true;
 }
 return super.isFocused();
 }
}

重写了isFocused方法,并进行判断,只有设置了marqueen属性的才保持选中状态,否则它就跟普通TextView一样。接下来就可以直接使用了,看下布局:

<LinearLayout 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"
 android:orientation="vertical">
 <FrameLayout
 android:id="@+id/titlebar_layout"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="#39ac69" >
 <LinearLayout

  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:background="#ffffff"
  android:gravity="center_vertical"
  android:orientation="horizontal" >

  <ImageView
  android:id="@+id/home_location_iv"
  android:layout_width="25dp"
  android:layout_height="27dp"
  android:layout_marginLeft="10dp"
  android:scaleType="fitXY"
  android:src="@drawable/icon_place" />

  <com.lxj.marqueetextview.MarqueeTextView
  android:id="@+id/home_location_tv"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_marginLeft="10dp"
  android:layout_marginRight="10dp"
  android:layout_weight="1"
  android:ellipsize="marquee"
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:gravity="center"
  android:marqueeRepeatLimit="marquee_forever"
  android:scrollHorizontally="true"
  android:singleLine="true"
  android:text="正在定位..."
  android:textColor="#39ac69"
  android:textSize="18sp" />

  <ImageView
  android:id="@+id/home_search_iv"
  android:layout_width="25dp"
  android:layout_height="27dp"
  android:layout_marginRight="10dp"
  android:scaleType="fitXY"
  android:src="@drawable/icon_place" />
 </LinearLayout>
</FrameLayout>
</LinearLayout>

要注意两点ellipsize属性要设置为”marquee”,行数属性即singleLine要设置为true。到此TextView的跑马灯效果就实现了。

希望本文对大家学习Android软件编程有所帮助。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多