Android:textview automatic scrolling (marquee)

Source: Internet
Author: User
Tags word wrap

Android:textview automatic scrolling (marquee)

TextView implementation of text scrolling requires the following points: 1. Text length is longer than can be displayed range: Android:singleline= "true" 2. Set scroll to, or display style: android:ellipsize= "marquee" 3. TextView The hidden text is only scrolled when the focus is taken, so you need to create a new class in the package to inherit the TextView. Overriding the IsFocused method, the default behavior of this method is that if TextView gets the focus, the method returns true and loses focus returns false. The marquee effect is also evaluated using this method to determine if the focus is obtained, so the return value is always set to true. The following goes from others: the Java language:Alwaysmarqueetextview class
 Public classAlwaysmarqueetextviewextendsTextView { PublicAlwaysmarqueetextview (Context context) {Super(context);} PublicAlwaysmarqueetextview (Context context, AttributeSet attrs) {Super(context, attrs);} PublicAlwaysmarqueetextview (context context, AttributeSet attrs,intDefstyle) {Super(context, attrs, Defstyle);} @Override Public BooleanisFocused () {return true;}

Adding such a alwaysmarqueetextview to the layout XML file is something you just learned.

XML language:Layout.xml
<Com.examples.AlwaysMarqueeTextViewAndroid: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 Property
Sets how the control is displayed when the text is too long. The following values are set: "Start"-the ellipsis is displayed at the beginning; "End"-the ellipsis is displayed at the end; "Middle"-the ellipsis is displayed in the middle; "Marquee"--displayed in the form of a marquee (animation moves Sideways)

Eidttext and TextView content too long words word wrap, using android:ellipsize and android:singleine can be resolved, so that only one line.

EditText does not support marquee

Use the following:

in XML

Android:ellipsize = "End"  ellipsis at the end android:ellipsize = "Start" ellipsis at the beginning android:ellipsize = "Middle"     ellipsis in the middle of Android: Ellipsize = "marquee"  marquee android:singleline = "true"

Of course, you can also use code statements

Tv.setellipsize (TextUtils.TruncateAt.valueOf ("END")); Tv.setellipsize (TextUtils.TruncateAt.valueOf ("START")); Tv.setellipsize (TextUtils.TruncateAt.valueOf ("Middle")) Tv.setellipsize (TextUtils.TruncateAt.valueOf ("MARQUEE") )); Tv.setsingleline (true);

Marqueerepeatlimit Property
Sets the number of times to repeat scrolling when ellipsize specifies marquee, which is expressed indefinitely when set to Marquee_forever.

Focusable Property
What you are guessing is whether you can get the focus, and the same focusableintouchmode should be the focus when sliding.

Problem with view combination:

XML language:Combination View
<LinearLayoutxmlns:android= "Http://"android:orientation= "vertical"android:gravity= "Center_vertical"Android:background= "@drawable/f_background"Android:layout_width= "Fill_parent"android:focusable= "true"Android:layout_height= "50px"><TextViewAndroid:id= "@+id/info_text"android:focusable= "true"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "TestMarquee. "Android:textcolor= "@color/black"Android:singleline= "true"android:ellipsize= "Marquee"Android:marqueerepeatlimit= "3″android:textsize= "18SP"/><TextViewAndroid: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>

The above example of 2 TextView combination as a view, because the set of LinearLayout for focusable and TextView can not get focus, so that the textview of the marquee effect is not shown, Even if you set TextView's android:focusable= "true" is useless. This is the time to use the Addstatesfromchildren property, set this property in LinearLayout, 然后设置TextView的focusable=  "true"  就可以了.关于 addStatesFromChildren的说明:

Sets whether thisViewGroup‘s drawable states also include its children‘s drawable states.

Android:textview Auto-scroll (marquee) (GO)

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: 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.