Reference website: https://www.jianshu.com/p/e6c1b825d322
At first, use the following XML layout:
1 <TextView 2 android:id="@+id/tv_person_name" 3 android:layout_marginTop="16dp" 4 android:layout_width="125dp" 5 android:layout_height="wrap_content" 6 android:text="健康快乐的小..." 7 android:textSize="20sp" 8 android:textColor="#CE000000" 9 android:layout_alignParentLeft="true" 10 android:layout_marginLeft="85dp" 11 android:singleLine = "true" // single line set 12 android: ellipsize = "marquee" // marquee 13 is Android: marqueeRepeatLimit = "marquee_forever" // infinite loop 14 Android:'s focusable = "to true" // get the focus 15 Android: focusableInTouchMode = "to true" />
The result: Marquee realize the effect of temporarily, but easy to lose focus after multiple click event. And on Android4.4 realize there is a short pause.
The difference focusable and focusableInTouchMode: https://blog.csdn.net/SylG17/article/details/85047234
I tried to re-acquire focus by EvenBus, but with no eggs.
Later, using a custom marquee categories:
. 1 public class MarqueeTextView the extends AppCompatTextView { 2 / ** number of rolling * / . 3 Private int marqueeNum = -1; // -1 permanent loop, the number of cycles is greater than 0. ` . 4 public void setMarqueeNum ( int marqueeNum) { . 5 the this .marqueeNum = marqueeNum; . 6 } . 7 public MarqueeTextView (the Context context) { . 8 Super (context); . 9 setAttr (); 10 } . 11 publicMarqueeTextView (the Context context, AttributeSet attrs) { 12 is Super (context, attrs); 13 is setAttr (); 14 } 15 public MarqueeTextView (the Context context, AttributeSet attrs, int defStyle) { 16 Super (context, attrs, defStyle); . 17 setAttr (); 18 } 19 / ** 20 * always get the focus 21 times in focus state * Marquee in TextView will scroll 22 is * / 23 is @Override 24 public Boolean isFocused () { 25 return to true; 26 } 27 / ** 28 * Set the properties 29 * / 30 Private void setAttr () { 31 is the this .setEllipsize (TextUtils.TruncateAt.MARQUEE); // set Happy effects such as 32 the this .setMarqueeRepeatLimit (marqueeNum); // Marquee provided repetitions 33 is the this .setSingleLine ( to true ); // set single line 34 } 35 }
Marquee using custom controls:
1 <com.sz.cszj.intelligentrobot.cszjrobot.view.MarqueeTextView 2 android:id="@+id/tv_person_name" 3 android:layout_marginTop="16dp" 4 android:layout_width="125dp" 5 android:layout_height="wrap_content" 6 android:text="健康快乐的小..." 7 android:textSize="20sp" 8 android:textColor="#CE000000" 9 android:layout_alignParentLeft="true" 10 android:layout_marginLeft="85dp"/>
Effect: not out of focus, Marquee effect can be achieved. But still there Caton.