android textView跑馬燈效果

較長的文字不能換行顯示,需要跑馬燈效果
方法一:在XML文件中設置屬性
注意maxlLines = “1”和singleLine=”true”的區別前者會將多餘部分截去換到下一行顯示,而後者是將內容全部放到一行只是屏幕無法顯示而已所以此處應爲後者。同理此處不能設置寬度爲匹配父控件
做法:
1. 自定義控件

public class MarqueeText extends TextView {
    public MarqueeText(Context context) {
        super(context);
    }

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

    public MarqueeText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public boolean isFocused() {
        return true;//此處使得文本一直獲得焦點
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        //此處是爲了防止焦點改變而效果消失
    }
}
  1. 應用自定義控件
<com.example.textviewsample.MarqueeText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text"
        android:id="@+id/text_view1"
        android:ellipsize="marquee"
        android:singLine = "true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"/>
        //使跑馬燈一直不停
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章