Android ApiDemos example (94): Text-& gt; Marquee

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.