Why do YOU use the Horse-light effect? When you use TextView a lot, you end up with a lot of text being “… Indicates and possibly important information has been hidden, hence the horselight effect. What is TextView running lantern? TextView running is when you want the TextView to be displayed on a single line, but the text content is more than one line, automatically sliding from left to right is called running.

First, use the TextView property to implement

1. The layout file directly sets the data in the layout

    <TextView
        android:id="@+id/marquee_tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawableLeft="@drawable/icon"
        android:drawablePadding="12dp"
        android:ellipsize="marquee"
        android:gravity="center_vertical"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true"
        android:text="What this project is going to do is make this single, long, long, single line TextView look like a horse light."
        android:textSize="22sp"/>
Copy the code

2. Or Java code to set data:

marqueeTv.setText("xxxx..." ); marqueeTv.setFocusable(true); / / get focus marqueeTv. SetFocusableInTouchMode (true); marqueeTv.requestFocus();Copy the code

However, this method can not be used for two TextViews at the same time. Reason: TextView’s default isFocused() method is limited to one object by default. The solution: Create a new class MarqueeTextView that inherits TextView and has its isFocused() method return true. (Note: subclass MarqueeTextView needs to implement all three constructors of its parent class, otherwise there will be problems.)

Effect:

Two, use custom View implementation

1. Create a class that inherits TextView and overwrites isFocused. Return true
/ * * *@description: Custom Marquee textView *@author: Pan_Hui
 * @date: 2020/5/20 remained with * /

public class MarqueeView extends TextView {
    // Implement the three constructors of TextView
    public MarqueeView( Context context ) {
        super( context );
    }

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

    public MarqueeView( Context context,  AttributeSet attrs, int defStyleAttr ) {
        super( context, attrs, defStyleAttr );
    }
    // override isFocused()
    @Override
    public boolean isFocused(a) {
        return true; }}Copy the code
2. In the layout file, the default TextView control in the XML file is replaced with a customized TextView by the package name + class name
    <com.p.h.MarqueeView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:gravity="center_vertical"
        android:scrollHorizontally="True"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true" 
        android:text="Running lantern, here I come, Dahui! Huey! Huey! Huey! Huey! Huey!"
        android:textColor="@color/blue"
        android:textSize="25sp"/>
Copy the code

Android :singleLine=”true” limit TextView singleLine display Avoid a newline android: ellipsize = “marquee” with horizontal scrolling display (need to get the current focus) android: focusable = “true” get focus android: focusableInTouchMode = “true” In touch mode, you can get the focus marquee_forever, which means you can loop forever, or you can write a number to indicate the number of times and then you can set it setText in your Activity; ScrollHorizontally indicates whether the rolling direction is horizontal.

Good arrived at this, actually I return quite busy of, for example 0 accompany wife, accompany child, dozen dozen wife, dozen dozen child what don’t say roll!