Android textview 跑马灯文字滚动效果
设置如下TextView控件文件的XML:
<com.example.Mytext
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="20dp"
android:gravity="center"
android:singleLine="true"//限制行数为1行
android:ellipsize="marquee"//marquee 文字滚动
android:marqueeRepeatLimit="marquee_forever"//文字滚动次数:marquee_forever 无限次
android:focusable="true"//获取焦点
android:focusableInTouchMode="true"//获取触摸焦点
android:textColor="@color/red"
android:text="@string/text"
/>
有其它布局如ScrollView等抢占焦点,需要自定义控件获取焦点:
public class Mytext extends TextView {
public Mytext(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
if(focused){
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
if(hasWindowFocus){
super.onWindowFocusChanged(hasWindowFocus);
}
@Override
public boolean isFocused() {
return true;
}
}