本文实例为大家分享了android textview跑马灯效果的具体代码,供大家参考,具体内容如下
一、要点
设置四个属性
android:singleline="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableintouchmode="true"
直接在xml中使用
<textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleline="true" android:ellipsize="marquee" android:focusable="true" android:focusableintouchmode="true" android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" />
注意:singleline属性 不能换成 maxllines
二、复杂布局
在复杂的布局中可能不会实现跑马灯效果。例如如下布局中,就只有第一个textview会有跑马灯效果
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv1" android:singleline="true" android:ellipsize="marquee" android:focusable="true" android:focusableintouchmode="true" android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv1" android:layout_margintop="10dp" android:singleline="true" android:ellipsize="marquee" android:focusable="true" android:focusableintouchmode="true" android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" /> </relativelayout>
这时候就需要自定义view,实现跑马灯效果
自定义marqueetextview extents textview 重写isfocused()方法,返回true
public class marqueetext extends textview { public marqueetext(context context) { super(context); } public marqueetext(context context, @nullable attributeset attrs) { super(context, attrs); } public marqueetext(context context, @nullable attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); } @override public boolean isfocused() { return true; } }
布局中使用
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.dhj.marqueedemo.view.marqueetext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv1" android:singleline="true" android:ellipsize="marquee" android:focusable="true" android:focusableintouchmode="true" android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" /> <com.example.dhj.marqueedemo.view.marqueetext android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv1" android:layout_margintop="10dp" android:singleline="true" android:ellipsize="marquee" android:focusable="true" android:focusableintouchmode="true" android:text="人生是一场无休、无歇、无情的战斗,凡是要做个够得上称为人的人,都得时时向无形的敌人作战。" /> </relativelayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
LongClick原理、上下文菜单原理、EditText长按弹窗原理、WebView长按弹窗自定义、修复WebView全选重复bug ———————————————— 版权声明:本文为CSDN博主「
JobScheduler 实现 特定时间,特定条件(系统空闲,电池电量,磁盘空间 ……)下执行任务
温习Android基础知识——《第一行代码(第三版)》读书笔记 Chapter 10 Service
网友评论