Android基于TextView实现跑马灯效果

本文实例为大家分享了Android TextView实现跑马灯效果的具体代码,供大家参考,具体内容如下

当Layout中只有一个TextView需要实现跑马灯效果时,操作如下。
在Layout的TextView配置文件中增加

        android:ellipsize="marquee"

        android:focusable="true"

        android:focusableInTouchMode="true"

        android:singleLine="true"

以上四条属性,即可实现跑马灯效果。

当有多个TextView想实现跑马灯效果时,实现起来稍微复杂一些。
首先新建一个类,继承自TextView。

package com.example.project1;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewDebug.ExportedProperty;
import android.widget.TextView;

public class MyTextView extends TextView{

  public MyTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
  }

  public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
  }

  public MyTextView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
  }

  @Override
  public boolean isFocused() {
    // TODO Auto-generatd method stub
    return true;
  }
}

重写函数 isFocused(),使其始终return true。

将Layout文件中的TextView修改为com.example.project1.MyTextView,如下。

 <com.example.project1.MyTextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:singleLine="true"
    android:text="@string/longText" />

  <com.example.project1.MyTextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:singleLine="true"
    android:text="@string/longText" />

此时两个TextView都可呈现跑马灯效果。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • TextView实现跑马灯效果 就这么简单!

    一.方法 这里我们用两种方法来实现跑马灯效果,虽然实质上是一种 实质就是: 1.TextView调出跑马灯效果 2.TextView获取焦点 第一种: 1.TextView调出跑马灯效果 android:ellipsize="marquee" 2.TextView获取焦点 android:focusable="true" android:focusableInTouchMode="true" 说明: 这种方法如果界面上别的控件获取焦点的时候就会停

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

    本文为大家分享一个非常简单但又很常用的控件,跑马灯状态的TextView.当要显示的文本长度太长,又不想换行时用它来显示文本,一来可以完全的显示出文本,二来效果也挺酷,实现起来超级简单,所以,何乐不为.先看下效果图: 代码实现 TextView自带了跑马灯功能,只要把它的ellipsize属性设置为marquee就可以了.但有个前提,就是TextView要处于被选中状态才能有效果,看到这,我们就很自然的自定义一个控件,写出以下代码: public class MarqueeTextView ex

  • Android用过TextView实现跑马灯效果的示例

    以前就遇到过这个问题,今天重新拾起来. 跑马灯效果其实就是当文字超过TextView控件宽度的时候,使用滚动的方式显示出来: 方法1:(直接xml搞定) Android系统中TextView实现跑马灯效果,必须具备以下几个条件: 1.android:ellipsize="marquee": 2.TextView必须单行显示,且内容必须超出TextView宽度: 3.TextView要获得焦点才能滚动. xml代码如下: <TextView android:id="@+i

  • android TextView实现跑马灯效果

    本文实例为大家分享了android TextView跑马灯效果的具体代码,供大家参考,具体内容如下 一.要点 设置四个属性 android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" 直接在xml中使用 <TextView android:layout_

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

    本文实例讲述了Android TextView跑马灯效果实现方法.分享给大家供大家参考,具体如下: public class MyTextView extends TextView{ public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public MyTextView(Context context, A

  • Android自定义TextView跑马灯效果

    Android自带的跑马灯效果不太好控制,还必须要满足条件才能有效果,而且速度不受控制.前面我的博客中有一篇就是用Android自带的跑马灯效果的,但是基于不同的使用效果,这里在网上找到了一个更好的方法.沿用了作者的一些方法,但是添加了更好的扩展功能,和大家一起分享.这里面有控制往左往右两个方向的实现. 1.首先是简单的布局main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&quo

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

     条件: 1.android:ellipsize="marquee" 2.TextView必须单行显示,即内容必须超出TextView大小 3.TextView要获得焦点才能滚动 mTVText.setText("超过文本长度的数据"); mTVText.setSingleLine(true);设置单行显示 mTVText.setEllipsize(TruncateAt.MARQUEE);设置跑马灯显示效果 TextView.setHorizontallyScrol

  • Android使用TextView跑马灯效果

    老规矩,先上图看效果. 说明 TextView的跑马灯效果也就是指当你只想让TextView单行显示,可是文本内容却又超过一行时,自动从左往右慢慢滑动显示的效果就叫跑马灯效果. 其实,TextView实现跑马灯效果很简单,因为官方已经实现了,你只需要通过设置几个属性即可.而且,相关的资料其实网上也有一大堆了,之所以还写这篇博客出来是因为,网上好多人的博客都是只贴代码的啊,好一点的就是附带几张图片,可是这是动画效果啊,不动起来,谁知道跑马灯效果到底长什么样,到底是不是自己想要的效果啊(不会只有题主

  • Android中使用TextView实现文字跑马灯效果

    通常情况下我们想实现文字的走马灯效果需要在xml文件中这样设置 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:

  • Android基于TextView实现跑马灯效果

    本文实例为大家分享了Android TextView实现跑马灯效果的具体代码,供大家参考,具体内容如下 当Layout中只有一个TextView需要实现跑马灯效果时,操作如下. 在Layout的TextView配置文件中增加         android:ellipsize="marquee"         android:focusable="true"         android:focusableInTouchMode="true"

  • android使用TextView实现跑马灯效果

    本文实例为大家分享了android使用TextView实现跑马灯效果的具体代码,供大家参考,具体内容如下 先上效果图:此为静态图,实际动态中文字匀速向左滑动. 实现步骤: 第一步:创建好布局页面 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.c

  • Android用TextView实现跑马灯效果代码

    目录 [前言] 一.新手设置跑马灯效果 [关键点讲解] [总结] 二.高端玩家设置跑马灯效果 三.延伸阅读 总结 [前言] 在Textview设置的宽度有限,而需要显示的文字又比较多的情况下,往往需要给Textview设置跑马灯效果才能让用户完整地看到所有设置的文字,所以给TextView设置跑马灯效果的需求是很常见的 一.新手设置跑马灯效果 1.先在xml中给Textview设置好对应的属性 <TextView android:id="@+id/tv" android:layo

  • Android文本视图TextView实现跑马灯效果

    本文实例为大家分享了Android文本视图TextView实现跑马灯效果的具体代码,供大家参考,具体内容如下 MainActivity package com.example.junior; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import android.view.View; import android.widget.TextView; public class MarqueeA

  • android自定义View实现跑马灯效果

    android自带的TextView可以实现跑马灯效果,但是有很多的局限性:比如需要设置ellipsize="marquee",获取 focusable="true",设置singleLine="true",控件里的内容需要超过控件本身的长度,无法控制滚动速度和滚动暂停和继续滚动功能,各种限制导致用起来特别不顺手,几乎无法使用到生产环境中,在此背景下,需要自定义View实现跑马灯效果. 使用主要方法:自定义View重写onDraw方法,通过can

随机推荐