Android textview 跑馬燈文字滾動效果

yg3n 9年前發布 | 2K 次閱讀 Java Android

設置如下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;  
     }  
}

 本文由用戶 yg3n 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!