Android uses TextView to achieve a marquee effect

Source: Internet
Author: User

One, the first way:

  Implemented by the TextView control's own properties (but the disadvantage is that when multiple TextView want to achieve this effect, only the first one has an effect)

The implementation method adds the following four properties:

Android:singleline= "true"
Android:ellipsize= "Marquee"
Android:focusable= "true"
Android:focusableintouchmode= "true"

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" >        <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"Android:layout_margintop= "16DP"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:text= "@string/textview" />        <TextViewAndroid:layout_below= "@+id/textview1"Android:id= "@+id/textview2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"Android:layout_margintop= "16DP"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:text= "@string/textview" />        </LinearLayout>

Two, the second way:

Implemented through Java code (customizing a TextView Class)

Add the following four properties:

Android:singleline= "true"
Android:ellipsize= "Marquee"
Android:focusable= "true"
Android:focusableintouchmode= "true"

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical" >        <Com.muke.textview_edittext.textAndroid:id= "@+id/textview1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"Android:layout_margintop= "16DP"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:text= "@string/textview" />        <Com.muke.textview_edittext.textAndroid:layout_below= "@+id/textview1"Android:id= "@+id/textview2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"Android:layout_margintop= "16DP"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:text= "@string/textview" />        </LinearLayout>
 PackageCom.muke.textview_edittext;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.widget.TextView; Public classTextextendsTextView { Publictext (context context) {Super(context); }         Publictext (context context, AttributeSet attrs) {Super(context, attrs); }         PublicText (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle); }     PublicText (context context, AttributeSet attrs,intDefstyleattr,intdefstyleres) {        Super(context, Attrs, defstyleattr, defstyleres); }            //the point is that this method     Public BooleanisFocused () {return true; }}
 Package Com.muke.textview_edittext; Import Android.os.Bundle; Import android.app.Activity;  Public class extends activity{        @Override    protectedvoid  onCreate (Bundle Savedinstancestate) {        super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    }}

Android uses TextView to achieve a marquee effect

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.