Android跑马灯无焦点滚动

通过设置TextView的属性如`android:ellipsize="marquee"`和`android:singleLine="true"`可以实现跑马灯效果,但仅在获取焦点时滚动。要使跑马灯持续滚动,需自定义TextView类,重写`onFocusChanged`和`onWindowFocusChanged`方法,避免焦点变化影响滚动。在布局文件中使用自定义的MarqueeText控件可达成持续跑马灯效果。

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

如果对显示没有特别要求可以直接使用属性就能做到,在布局文件中将TextView属性设置一下:

<TextView  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:ellipsize="marquee"  
    android:focusable="true"  
    android:focusableInTouchMode="true"  
    android:marqueeRepeatLimit="marquee_forever"  
    android:scrollHorizontally="true"  
    android:singleLine="true"  
    android:text="哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"  
    android:textSize="17sp" />  


关键代码如下:

   //设置为跑马灯显示
           android:ellipsize="marquee"

           //获取焦点
          android:focusable="true"

          //可以通过toucth来获得focus
          android:focusableInTouchMode="true"

          //设置重复的次数
          android:marqueeRepeatLimit="marquee_forever"

          //单行显示文字
          android:singleLine="true"

使用属性设置要注意:这种属性配置只能在TextView获取焦点的时候跑马灯才会滚动,一旦失去焦点跑马灯就不会滚动
如果没有特殊需求的话使用这种还是简单方便的,但是如果需要跑马灯一直在滚动的话单纯的这样是做不到的,于是上网查了各种资料,大多数都是重定义TextView控件,重写onFocusChanged方法,里面什么也不做,将super的调用直接屏蔽,但经过测试这种还是不行,如果在dialog弹出时,显示效果和TextView属性配置是一样的结果,但是如果再在自定义的控件类中重写onWindowFocusChanged,将super方法屏蔽掉,这样就达到效果了,就算是dialog弹出也不影响跑马灯的显示效果,话不多说,先看图片:



下面看看具体代码,自定义的TextView(MarqueeText):

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

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

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

    public MarqueeText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    //重写isFocused方法,让其一直返回true
    public boolean isFocused() {
        return true;
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {

    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {

    }
}

看到很多博客都是自定义 MarqueeText,但是没有重写onWindowFocusChanged,当dialog弹出时,跑马灯就直接停掉了

在布局文件中的使用:

<com.test.view.MarqueeText
            android:id="@+id/redmin_delete_tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center_vertical"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"/>


进过测试,在MarqueeText没有获取焦点的时候也能滚动,这样虽然麻烦了些,但感觉比制作动画要稍微好一点,对于更酷炫的跑马灯效果,请参看这篇博客: Android自定义文字闪烁渐变色的跑马灯

编码的路还很长,我们一起走

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值