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

 更新时间:2020年07月29日 13:27:29   作者:showCar  
这篇文章主要介绍了Android TextView跑马灯效果实现方法,涉及Android布局文件中相关属性的设置技巧,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文为大家分享一个非常简单但又很常用的控件,跑马灯状态的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自定义View之酷炫数字圆环

    Android自定义View之酷炫数字圆环

    这篇文章主要为大家详细介绍了Android自定义View之酷炫数字圆环,实现效果很酷,,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • Android View 事件防抖的两种方案

    Android View 事件防抖的两种方案

    这篇文章主要介绍了Android View 事件防抖的两种方案,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下
    2021-04-04
  • Room Kotlin API的使用入门教程

    Room Kotlin API的使用入门教程

    这篇文章主要介绍了Room Kotlin API使用入门教程,帮助大家更好的理解和学习使用并且测试 Room Kotlin API,感兴趣的朋友可以了解下
    2021-04-04
  • Android仿微信语音聊天界面设计

    Android仿微信语音聊天界面设计

    这篇文章主要为大家详细介绍了Android仿微信语音聊天界面设计代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • 详解Android 蓝牙通信方式总结

    详解Android 蓝牙通信方式总结

    这篇文章主要介绍了详解Android 蓝牙通信方式总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2013-11-11
  • 移动端开发之Jetpack Hilt技术实现解耦

    移动端开发之Jetpack Hilt技术实现解耦

    Hilt的出现解决前两点问题,因为Hilt是Dagger针对Android平台的场景化框架,比如Dagger需要我们手动声明注入的地方,而Android声明的地方不都在onCreate()吗,所以Hilt就帮我们做了,除此之外还做了很多事情
    2023-02-02
  • Android使用动画设置ProgressBar进度的方法

    Android使用动画设置ProgressBar进度的方法

    这篇文章主要为大家详细介绍了Android使用动画设置ProgressBar进度的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • Android GridView简单实例

    Android GridView简单实例

    这篇文章主要为大家详细介绍了Android GridView简单实例,简单实现九宫格效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Android中使用开源框架Citypickerview实现省市区三级联动选择

    Android中使用开源框架Citypickerview实现省市区三级联动选择

    这篇文章主要介绍了Android中使用开源框架Citypickerview实现省市区三级联动选择效果,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-03-03
  • Android实现沉浸式通知栏通知栏背景颜色跟随app导航栏背景颜色而改变

    Android实现沉浸式通知栏通知栏背景颜色跟随app导航栏背景颜色而改变

    这篇文章主要介绍了Android实现沉浸式通知栏通知栏背景颜色跟随app导航栏背景颜色而改变的相关资料,需要的朋友可以参考下
    2016-02-02

最新评论