Android TextView设置跑马灯无效?

一、想要效果

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

二、出现问题

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

          <TextView
                    android:id="@+id/outdoor_weather_tips_tv"
                    android:layout_width="192dp"
                    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:textSize="12sp" />

跑马灯效果属性介绍: 

       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;
        }
    }

     

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FrancisBingo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值