Android——跑马灯效果

来源:互联网 发布:自学java好还是培训好 编辑:程序博客网 时间:2024/05/22 10:38

跑马灯效果:

这里写图片描述

文字会慢慢走,这就是所谓的跑马灯效果。

需要在布局文件中添加几个属性:

<TextView    android:id="@+id/tv"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="世界上最远的距离,不是爱,不是恨,而是熟悉的人,渐渐变得陌生。"    android:singleLine="true"    android:ellipsize="marquee"    android:focusable="true"    android:focusableInTouchMode="true"    />

但如果有多行文字,其实仅这些还不够实现,只有第一行会实现跑马灯,其他的不动,所以需要自定义view,将TextView稍加改

动。

新建一个类:

package com.example.paomadeng;import android.content.Context;import android.util.AttributeSet;import android.widget.TextView;public class MarqueeText extends TextView{    public MarqueeText(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        // TODO Auto-generated constructor stub    }    public MarqueeText(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub    }    public MarqueeText(Context context) {        super(context);        // TODO Auto-generated constructor stub    }    @Override    public boolean isFocused() {        // TODO Auto-generated method stub        return true;    }}

isFocused设置为true,这样com.example.paomadeng.MarqueeText这个就可以替代TextView,

布局文件:

<com.example.paomadeng.MarqueeText    android:id="@+id/tv"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="世界上最远的距离,不是爱,不是恨,而是熟悉的人,渐渐变得陌生。"    android:singleLine="true"    android:ellipsize="marquee"    android:focusable="true"    android:focusableInTouchMode="true"    />

这样就可以实现多行文字跑马灯 了。。。

1 0