First, the main points
Set four properties
Android:singleline= "true"
Android:ellipsize= "Marquee"
Android:focusable= "true"
Android:focusableintouchmode= "true"
Use directly in XML
<TextView android:layout_width= "Wrap_content" android:layout_height= "Wrap_content " android:singleline=" true " android:ellipsize=" marquee " android:focusable = "true" android:focusableintouchmode= "true" android:text= "Life is an endless, no-break, The ruthless battle, the person who wants to be a merited called human, must always fight to the invisible enemy. "/>
Note: The Singleline property cannot be replaced with Maxllines
Second, complex layout
The marquee effect may not be achieved in complex layouts. For example, in the following layout, only the first TextView will have a marquee effect.
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/tv1"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:text= "Life is an endless, non-stop, ruthless fight, and anyone who wants to be a merited called a man, must always fight against the invisible enemy." "/> <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/tv1"Android:layout_margintop= "10DP"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:text= "Life is an endless, non-stop, ruthless fight, and anyone who wants to be a merited called a man, must always fight against the invisible enemy." "/></relativelayout>
This time you need to customize the view to achieve the marquee effect
The custom Marqueetextview extents TextView overrides the IsFocused () method, which returns true
Public classMarqueeTextextendsTextView { PublicMarqueeText (Context context) {Super(context); } PublicMarqueeText (Context context, @Nullable AttributeSet attrs) {Super(context, attrs); } PublicMarqueeText (context context, @Nullable AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); } @Override Public BooleanisFocused () {return true; }}
Use in layouts
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <Com.example.dhj.marqueedemo.View.MarqueeText android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/tv1"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:text= "Life is an endless, non-stop, ruthless fight, and anyone who wants to be a merited called a man, must always fight against the invisible enemy." "/> <Com.example.dhj.marqueedemo.View.MarqueeText android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/tv1"Android:layout_margintop= "10DP"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:text= "Life is an endless, non-stop, ruthless fight, and anyone who wants to be a merited called a man, must always fight against the invisible enemy." "/></relativelayout>
Android TextView Marquee Effect