Android——跑马灯效果

跑马灯效果:

这里写图片描述

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

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

<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"
    />

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值