Android中设置文字跑马灯效果不动

在跟着B站Up主天哥在奔跑,学习Android的TextView控件时,想要实现一个跑马灯效果,但是按照视频中的代码发现文字是不会动的。视频中的代码如下:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_7"
        android:text="天哥在奔跑(跑马灯效果)天哥在奔跑(跑马灯效果)天哥在奔跑(跑马灯效果)天哥在奔跑(跑马灯效果)"
        android:textSize="24sp"
        android:textColor="#000000"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true">
</TextView>

然后经过弹幕的提醒,可能是跑马效果所需要的焦点被其他控件抢走了,于是,在onCreate函数里添加下面两行代码

		mTv_7 = findViewById(R.id.tv_7);
        mTv_7.setSelected(true);

这两行代码作用是根据id,找到控件,然后将焦点设置到该控件。经过编译运行之后,实现了文字跑马灯的效果。
在这里插入图片描述

原创文章 1 获赞 1 访问量 157

猜你喜欢

转载自blog.csdn.net/qq_36347651/article/details/105867512