TextView设置跑马灯效果

本文介绍如何在Android中实现TextView内的文本自动滚动效果。主要通过设置TextView的特定属性,包括使用ellipsize属性设置为marquee样式,确保TextView的文本宽度超出其本身宽度,并启用TextView的焦点获取功能。同时提供了自定义TextView类MarkqueeText的具体实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

若要让TextView里的文本滚动,必须满足以下几个因素:

1.TextView里文本宽度超过TextView的宽度
android:singleLine=”true”(已过时)
android:lines=”1”

2.设置ellipsize属性
android:ellipsize=”marquee”

3.只有在TextView获取到焦点时,才会滚动.所以加上
android:focusableInTouchMode=”true”
android:focusable=”true”

4.滚动重复次数设置:
android:marqueeRepeatLimit=”marquee_forever”(1代表1次,-1代表无限循环。)

自定义控件MarkqueeText重写父类方法

public class MarkqueeText extends TextView {
    public MarkqueeText(Context context) {
        super(context);
    }

    public MarkqueeText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MarkqueeText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

Activity代码:

MarkqueeText.setHorizontallyScrolling(true); //让文字可以水平滑动
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值