TextView文字横向滚动(跑马灯效果)

本文介绍如何使用TextView实现文字的跑马灯滚动效果。主要通过设置singleLine、focusable及ellipsize属性来实现文字的横向滚动。此外,还提供了一个AlwaysMarqueeTextView类,确保TextView始终处于聚焦状态。

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

TextView实现文字滚动需要以下几个要点:
1、文字长度长于可显示范围:android:singleLine="true";
2、设置可滚到,或显示样式:android:ellipsize="marquee";
3、TextView只有在获取焦点后才会滚动显示隐藏文字,所以可以重写TextView类。(但是一直给予焦点可能会导致其不能被点击,如放在listView中的时候)

public class AlwaysMarqueeTextView extends TextView{

	public AlwaysMarqueeTextView(Context context) {
		super(context);
	}
	
	public AlwaysMarqueeTextView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

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

	//始终返回true,即一直获得焦点
	@Override
	public boolean isFocused() {
		return true;
	}
}


布局文件中的TextView添加:

android:singleLine =“true”
android:focusable=“true”
android:marqueeRepeatLimit=“marquee_forever” //滚动次数,此时为无数次
android:ellipsize =“marquee”
ellipsize属性
设置当文字过长时,该控件该如何显示。有如下值设置:”start”—–省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值