The screen of a mobile phone is relatively small. Sometimes you need to use a text box (TextView or its subclass) to display a long line of text. One way is to cut the text, use... .
A common practice is to use Marquee to scroll text. This example describes the Marquee Effect of TextView.
Let's take a look at the definition of marquee. xml in layout:
[Html]
<Button
Android: layout_width = "150dip"
Android: layout_height = "wrap_content"
Android: text = "@ string/marquee_default"
Android: singleLine = "true"
Android: ellipsize = "marquee"/>
<Button
Android: layout_width = "150dip"
Android: layout_height = "wrap_content"
Android: text = "@ string/marquee_once"
Android: singleLine = "true"
Android: ellipsize = "marquee"
Android: marqueeRepeatLimit = "1"/>
<Button
Android: layout_width = "150dip"
Android: layout_height = "wrap_content"
Android: text = "@ string/marquee_forever"
Android: singleLine = "true"
Android: ellipsize = "marquee"
Android: marqueeRepeatLimit = "marquee_forever"/>
<Com. example. android. apis. text. ScrollAlwaysTextView
Android: layout_width = "150dip"
Android: layout_height = "wrap_content"
Android: text = "@ string/marquee_forever"
Android: singleLine = "true"
Android: ellipsize = "marquee"
Android: marqueeRepeatLimit = "marquee_forever"/>
<Button
Android: layout_width = "150dip"
Android: layout_height = "wrap_content"
Android: text = "@ string/marquee_default"
Android: singleLine = "true"
Android: ellipsize = "marquee"/>
<Button
Android: layout_width = "150dip"
Android: layout_height = "wrap_content"
Android: text = "@ string/marquee_once"
Android: singleLine = "true"
Android: ellipsize = "marquee"
Android: marqueeRepeatLimit = "1"/>
<Button
Android: layout_width = "150dip"
Android: layout_height = "wrap_content"
Android: text = "@ string/marquee_forever"
Android: singleLine = "true"
Android: ellipsize = "marquee"
Android: marqueeRepeatLimit = "marquee_forever"/>
<Com. example. android. apis. text. ScrollAlwaysTextView
Android: layout_width = "150dip"
Android: layout_height = "wrap_content"
Android: text = "@ string/marquee_forever"
Android: singleLine = "true"
Android: ellipsize = "marquee"
Android: marqueeRepeatLimit = "marquee_forever"/>
The effect of the lantern is mainly configured through the android: singleLine, android: ellipsize, android: marqueeRepeatLimit attributes.
Android: singleLine = true: indicates that a single line of text is used. If multiple lines of text are used, Marquee is not used.
Android: marqueeRepeatLimit.
Android: ellipsize: sets how to cut the text when the text is too long. The options include none, start, middle, and end. If you use the marquee, set it to marquee.
However, the default behavior of Android is that the Focus effect is displayed only when the control obtains the Focus. In this example, the Button is used, and the text on the Button is displayed only when the Focus is obtained by a Button.
In some cases, it is necessary to keep the text rolling to attract the user's attention. This is to use the derived TextView, The onFocusChanged, onWindowFocusChanged, and isFocused methods can be reloaded.
Modify this example to add a ScrollAlwaysTextView class:
[Java]
Public class ScrollAlwaysTextView extends TextView {
Public ScrollAlwaysTextView (Context context ){
This (context, null );
}
Public ScrollAlwaysTextView (Context context, AttributeSet attrs ){
This (context, attrs, android. R. attr. textViewStyle );
}
Public ScrollAlwaysTextView (Context context, AttributeSet attrs,
Int defStyle ){
Super (context, attrs, defStyle );
}
@ Override
Protected void onFocusChanged (boolean focused, int direction,
Rect previuslyfocusedrect ){
If (focused)
Super. onFocusChanged (focused, direction, previuslyfocusedrect );
}
@ Override
Public void onWindowFocusChanged (boolean focused ){
If (focused)
Super. onWindowFocusChanged (focused );
}
@ Override
Public boolean isFocused (){
Return true;
}
}
Public class ScrollAlwaysTextView extends TextView {
Public ScrollAlwaysTextView (Context context ){
This (context, null );
}
Public ScrollAlwaysTextView (Context context, AttributeSet attrs ){
This (context, attrs, android. R. attr. textViewStyle );
}
Public ScrollAlwaysTextView (Context context, AttributeSet attrs,
Int defStyle ){
Super (context, attrs, defStyle );
}
@ Override
Protected void onFocusChanged (boolean focused, int direction,
Rect previuslyfocusedrect ){
If (focused)
Super. onFocusChanged (focused, direction, previuslyfocusedrect );
}
@ Override
Public void onWindowFocusChanged (boolean focused ){
If (focused)
Super. onWindowFocusChanged (focused );
}
@ Override
Public boolean isFocused (){
Return true;
}
}
When you use this class to display text, the text will always scroll.
Author: mapdigit