This article describes the implementation method of text-scrolling display in Android development. Share it for your reference, as follows:
When using TextView in a project, there are always problems that we need to deal with due to too much content to be displayed. The first thing we thought of was TextViewandroid:ellipsize
Attributes, such asandroid:ellipsize="end"
, the effect is to type three small dots at the end of the text.
However, this property must be used with android:singLine="true". Generally speaking, it is easier to implement the ellipsis form of the three points at the end.
If we require all text to be displayed, but in order to save the beautiful UI interface, how to display all super long text in a TextView of limited size? We thought of letting the text scroll.
Everyone also thought about it throughandroid:ellipsize="marquee"
To implement it, but I have no effect when using this in my project.
Cooperatedandroid:singLine="true"
The text scrolling display is also not possible. There are many solutions online, almost all about the focus.
For example, someone suggested adding TextView in the layout fileandroid:focusable="true"
, but sometimes it has no effect.
My way of handling it is to rewrite the TextView, just simply change a 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() { // Just return true here return true; } }
Then write the rewrite TextView as a control in the layout file and add:
android:marqueeRepeatLimit="marquee_forever" android:ellipsize="marquee" android:singleLine="true"
If you are aware of the attributes, of course, don’t forget to write the necessary attributes such as width, height, etc.
Finally, when using the TextView, you need to add another sentence:
MarqueTextView tv=(MarqueTextView)findViewById(.my_text_view); (true);
The scrolling effect is available.
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.