-->

Marquee using maxLines

2020-04-10 00:53发布

问题:

How can I have a marquee using MaxLines instead of SingleLine ?

This is my TextView :

<TextView
    android:text="bla bla bla bla bla bla"
    android:id="@+id/MarqueeText" 
    android:layout_width="30dp"
    android:layout_height="wrap_content" 
    android:singleLine="true"
    android:ellipsize="marquee" 
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:freezesText="true">

After in my code.java I setSelected my TextView :

TextView txtView=(TextView) findViewById(R.id.MarqueeText);
txtView.setSelected(true);

The problem is android:singleLine is deprecated so I have to use android:maxLines instead but the marquee don't work with it.

回答1:

You can try using this :

android:maxLength = "10"

OR

After setting android:maxLines="1", you have to set your inputType too. So, set your android:inputType="text" and that should do the trick.



回答2:

In XML

<TextView
    android:text="11111111111111111111111111111111111111111111111111111111111111"
    android:id="@+id/text_marquee"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    />

In Java

((TextView)findViewById(R.id.text_marquee)).setHorizontallyScrolling(true);
((TextView)findViewById(R.id.text_marquee)).setSelected(true);