Android TextView设置跑马灯无效?

一、想要效果

想让Android的TextView实现跑马灯效果,垂直或者横向文字滚动。

二、出现问题

设置了跑马灯相关属性后发现没有达到滚动的效果,在xml中设置属性如下

          

跑马灯效果属性介绍: 

       android:ellipsize="marquee"//文字显示不完全,以什么方式显示(marquee代码滚动)

  1. android:focusable="true"//获得焦点

  2. android:focusableInTouchMode="true"//获得触摸焦点

  3. android:marqueeRepeatLimit="marquee_forever"//滚动模式

  4. android:scrollHorizontally="true"//横向滚动

  5. android:singleLine="true"//以单行文本显示

  6. //几个不同的值

  7. android:ellipsize="start"//开头以...隐藏多余文字

  8. middle//中间以...隐藏多余文字

  9. end//结尾以...隐藏多余文字

注意:显示的文字必须要超出给定的宽度
 

三、原因分析

由于界面view太多,导致这个TextView就不一定能够获取到焦点,获取不到焦点也就没有跑马灯效果了

四、解决方案

  1. /**
     * Created by francisbingo on 2019-09-26 10:44
     *
     * view层级太多,为了实现跑马灯效果的自定义view
     */
    public class MarqueeTextView extends TextView {
        public MarqueeTextView(Context context) {
            super(context);
        }
    
        public MarqueeTextView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MarqueeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public boolean isFocused() {
            return true;
        }
    }

     

你可能感兴趣的:(Android)