android使用TextView实现跑马灯的效果

Posted 我不是乘树猿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android使用TextView实现跑马灯的效果相关的知识,希望对你有一定的参考价值。

android使用TextView实现跑马灯的效果

1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.imooc.marqueetextviewdemo.MainActivity">

<TextView
        android:id="@+id/tv_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name" />

</RelativeLayout>

2.text文字内容:
strings.xml:
<resources>
<string name="app_name">MarqueeTextViewDemo</string>
<string name="name">MarqueeTextViewDemo,MarqueeTextViewDemo,MarqueeTextViewDemo,MarqueeTextViewDemo</string>
</resources>

图例:

未设置任何属性之前文字过多,自动换行显示。

3.设置单行跑马灯
只需要在TextView中添加4条属性

android:singleLine="true"  //设置文字为一行显示,未能显示的文字用三个“...”显示

android:ellipsize="marquee"  //去掉三个“...”

android:focusable="true"  
android:focusableInTouchMode="true"

完整代码例:
<TextView
android:id="@+id/tv_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
/>

图例:

未运行layout里显示样式,整个为一行。

运行效果图例:

文字开始跑马灯了。

以上是关于android使用TextView实现跑马灯的效果的主要内容,如果未能解决你的问题,请参考以下文章

Android 使用TextView实现跑马灯效果

Android TextView跑马灯效果

android TextView实现跑马灯效果(字体滚动)

Android 文字自动滚动(跑马灯)效果的两种实现方法[特别好使]

android TextView实现跑马灯效果

android:TextView实现文字走马灯效果(欺骗系统获取持久的焦点)