Android TextView text scrolls horizontally (marquee)

TextView realizes text scrolling requires the following points:

1. The length of the text is longer than the displayable range: android:singleLine=”true”
2. Set the scrollable to, or display style: android:ellipsize=”marquee” 3. The
TextView will scroll to display the hidden text only after getting the focus, so You need to create a new class in the package that inherits TextView. Override the isFocused method. The default behavior of this method is that if the TextView gains focus, the method returns true, and if it loses focus, it returns false. The marquee effect estimation also uses this method to determine whether the focus is obtained, so its return value is always set to true.

public class AlwaysMarqueeTextView extends TextView {

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

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

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

@Override
public boolean isFocused() {
return true;
}

Add such an AlwaysMarqueeTextView to the layout XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:focusable="true"
    tools:context=".MainActivity">

    <com.example.administrator.marqueeprojecttest.AlwaysMarqueeTextView
        android:layout_marginTop="45dp"
        android:id="@+id/AMTV1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="@string/zhouenlai" />

    <com.example.administrator.marqueeprojecttest.AlwaysMarqueeTextView
        android:layout_marginTop="45dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="@string/zhouenlai2"
        />
</LinearLayout>

The ellipsize property
sets how the control should be displayed when the text is too long. There are the following value settings: "start"--the ellipsis is displayed at the beginning; "end"--the ellipsis is displayed at the end; "middle"--the ellipsis is displayed in the middle; "marquee"--displayed as a marquee (animation moves horizontally) )

marqueeRepeatLimit property
In the case of ellipsize specifying marquee, set the number of repeated scrolls, when set to marquee_forever, it means infinite times.

The focusable property
I guessed by myself, it should be whether it can get the focus, and the focusableInTouchMode should be whether it can get the focus when sliding.

Reference link
Source code download address: https://download.csdn.net/download/qq_26296197/10370470
https://blog.csdn.net/muyu114/article/details/6400563

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324885205&siteId=291194637