android textview 走馬燈設計

2.TextView實現跑馬燈效果

<TextView android:layout_height="wrap_content"
android:layout_width="200dip"
android:id="@+id/textView1"
android:text="" android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusableInTouchMode="true"
android:focusable="true">


但是這樣子有一個缺點,就是這種狀態的跑馬燈只能在TextView處於焦點狀態的時候,它纔會滾動,對於實際的開發應用中很不實用,爲了是跑馬燈無論在什麼情況下都能跑起來,這裏需要自定義一個TextView,它繼承TextView,並且重寫isFocuse()方法,讓它永遠返回true,這樣跑馬燈效果就能一直的跑起來了。

public class MarqueeTextView extends TextView {

 public MarqueeTextView(Context context) {
  super(context);
 }
 
 public MarqueeTextView(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

 public MarqueeTextView(Context context, AttributeSet attrs,
   int defStyle) {
  super(context, attrs, defStyle);
 }
 
 @Override
 public boolean isFocused() {
  return true;
 }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章