在xml布局文件中添加

android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"

这些属性
即:

    <TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:text="@string/hello_world" />

如果是多个TextView,则需要把定义一个类继承TextView,并且把TextView改成包名+自己定义的类名
代码示例如下:

  <com.sunxiaoning.marqueetextviewdemo.MarqueeText
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:text="@string/hello_world" />
 <com.sunxiaoning.marqueetextviewdemo.MarqueeText
    android:layout_below="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:text="@string/hello_world" />

同时自定义一个类,继承TextView重写TextView的所有方法和isFocused()方法,使isFocused()的返回值为true

package com.sunxiaoning.marqueetextviewdemo;

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

public class MarqueeText extends TextView{

    public MarqueeTexct(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        
    }

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

    public MarqueeTexct(Context context) {
        super(context);
        
    }

    public boolean isFocused() {
        return true;
    }

}

Last modification:October 7, 2017
If you think my article is useful to you, please feel free to appreciate