Android重写TextView实现文字跑马灯效果

本文介绍如何使用自定义的MarqueeTextView类实现文字跑马灯效果,并提供了XML配置示例,包括关键属性设置。

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

/**
 * 重写TextView实现文字跑马灯效果
 */

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

/**@Discription:
 * @author Rose
 */
public class MarqueeTextView extends TextView{
	
	private boolean scroll=false;
	/**
	 * @param context
	 * @param attrs
	 */
	public MarqueeTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
	    if(focused)
	        super.onFocusChanged(focused, direction, previouslyFocusedRect);
	}

	@Override
	public void onWindowFocusChanged(boolean hasWindowFocus) {
		if(hasWindowFocus)
			super.onWindowFocusChanged(hasWindowFocus);
	}
	
	@Override
	public boolean isFocused() {
	    return scroll;
	}

	/**
	 * @return the scroll
	 */
	public boolean isScroll() {
		return scroll;
	}

	/**
	 * @param scroll the scroll to set
	 */
	public void setScroll(boolean scroll) {
		this.scroll = scroll;
	}
}


第一步:

1.实现以上的MarqueeTextView.

2.通过setScroll(true)来开启跑马灯。


第二步 :

1.xml中Text修改为

        <com.包名.MarqueeTextView
            android:id="@+id/episode"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:gravity="center"
            android:marqueeRepeatLimit="marquee_forever"
            android:singleLine="true"
            android:text="第1集"
            android:textColor="@color/white"
            android:textSize="16sp" />
其中重要属性有:

<1> android:ellipsize="marquee"

<2>android:marqueeRepeatLimit="marquee_forever"

<3>android:singleLine="true"






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值