Analysis of the Realization Method of Extremely Long Scroll Display of TextView Text in the Development of Android

  • 2021-08-17 01:05:24
  • OfStack

In this paper, an example is given to describe the implementation method of TextView text over-long scrolling display in Android development. Share it for your reference, as follows:

When using TextView in a project, there will always be problems that we need to deal with because of too much content to display. What we thought of in the first time was TextView android:ellipsize Attributes, such as android:ellipsize="end" The effect is to type 3 dots at the end of the text.

But this attribute should be used in conjunction with android: singLine = "true". Generally speaking, it is relatively easy to realize the ellipsis form of the last three points.

If all the text is required to be displayed, but in order to save the beautiful interface of UI, how to display all the ultra-long text in the limited size TextView, we thought of scrolling the text.

Everyone also thought of passing android:ellipsize="marquee" However, I have no effect when I use this in my project.

Cooperate with android:singLine="true" Also 1 can't realize text scrolling display. There are many solutions on the Internet, almost all of which talk about the focus.

For example, it is suggested to add TextView in the layout file android:focusable="true" But sometimes it doesn't work.

My approach is to overwrite TextView by simply changing one code:


public class MarqueTextView extends TextView {
  public MarqueTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }
  public MarqueTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
  public MarqueTextView(Context context) {
    super(context);
  }
  @Override
  public boolean isFocused() {
  // Is to return here true You can 
    return true;
  }
}

Then write the replicated TextView as a control in the layout file and add:


android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
android:singleLine="true"

And other attributes, of course, don't forget to write width, height and other necessary attributes.

Finally, when using the TextView, add another sentence:


MarqueTextView tv=(MarqueTextView)findViewById(R.id.my_text_view);
tv.setSelected(true);

The scrolling effect will be there.

More readers interested in Android can check out the topics on this site: "Introduction and Advanced Tutorial of Android Development", "Summary of Android Debugging Skills and Common Problem Solutions", "Summary of Android Basic Component Usage", "Summary of Android View View Skills", "Summary of Android Layout layout Skills" and "Summary of Android Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: