当前位置: 移动技术网 > IT编程>移动开发>Android > Android基于TextView实现跑马灯效果

Android基于TextView实现跑马灯效果

2019年07月24日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了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都可呈现跑马灯效果。

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网