本文实例为大家分享了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都可呈现跑马灯效果。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android studio开发小型对话机器人app(实例代码)
Android通过Java sdk的方式接入OpenCv的方法
Android 通过cmake的方式接入opencv的方法步骤
Android Studio finish()方法的使用与解决app点击“返回”(直接退出)
Android 进度条 ProgressBar的实现代码(隐藏、出现、加载进度)
网友评论