Android TextView text horizontal auto-scroll (marquee)

Source: Internet
Author: User

TextView provides the following key points for text scrolling:
1. The text length is longer than the display range: android: singleLine = "true"
2. Set to be rolled to or display style: android: ellipsize = "marquee"
3. TextView only scrolls to display hidden text after obtaining the focus. Therefore, you need to create a new class in the package to inherit TextView. Override the isFocused method. The default behavior of this method is: If TextView gets the focus, the method returns true, and false if the focus is lost. This method is also used to determine whether to obtain the focus, so the return value is always set to true.


Java: AlwaysMarqueeTextView class
Public class AlwaysMarqueeTextView extends TextView {

Public AlwaysMarqueeTextView (Context context ){
Super (context );
}

Public AlwaysMarqueeTextView (Context context, AttributeSet attrs ){
Super (context, attrs );
}

Public AlwaysMarqueeTextView (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
}

@ Override
Public boolean isFocused (){
Return true;
}

Adding such an AlwaysMarqueeTextView to the layout XML file is also just learned.

XML language: layout. xml
<Com. examples. AlwaysMarqueeTextView
Android: id = "@ + id/AMTV1 ″
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: lines = "1 ″
Android: focusable = "true"
Android: focusableInTouchMode = "true"
Android: scrollHorizontally = "true"
Android: marqueeRepeatLimit = "marquee_forever"
Android: ellipsize = "marquee"
Android: background = "@ android: color/transparent"
/>
Ellipsize attribute
Set how to display the control when the text is too long. You can set the following values: "start" -- ellipsis () at the beginning, "end" -- ellipsis () at the end, and "middle" -- ellipsis () in the middle; "marquee"-displayed as a marquee (horizontal movement of the animation)

MarqueeRepeatLimit attributes
When ellipsize specifies marquee, it sets the number of repeated scrolling times. When it is set to marquee_forever, it indicates unlimited times.

View combination:

XML language: Combined View
<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: gravity = "center_vertical"
Android: background = "@ drawable/f_background"
Android: layout_width = "fill_parent"
Android: focusable = "true"
Android: layout_height = "50px">
<TextView
Android: id = "@ + id/info_text"
Android: focusable = "true"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "test marquee..."
Android: textColor = "@ color/black"
Android: singleLine = "true"
Android: ellipsize = "marquee"
Android: marqueeRepeatLimit = "3 ″
Android: textSize = "18sp"
/>
<TextView
Android: id = "@ + id/date_text"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "bottom"
Android: textColor = "@ color/gray"
Android: text = "2010/05/28 ″
Android: textSize = "12sp"
/>
</LinearLayout>
In the preceding example, the two textviews are combined into a View. Because LinearLayout is set to focusable, TextView cannot get the focus, so that the marquee effect of this TextView cannot be displayed, even if you set the android: focusable = "true" of TextView, it is useless. in this case, you need to use the addStatesFromChildren attribute. Set this attribute in LinearLayout and set the TextView's focusable = "true. description of addStatesFromChildren:

Sets whether this ViewGroup's drawable states also include its children's drawable states.



From the Central-Perk Column

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.