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