textView跑馬燈效果實現

自定義一個MyMarqueeTextView extends TextView,重寫他的三個構造方法,還有要重寫isFocused方法,並返回true

{

public AlwaysMarqueeTextView(Context context{
super(context);
}

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

public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle{
super(context, attrs, defStyle);
}

@Override
public boolean isFocused() {
return true;
}


textView在佈局文件中設置如下屬性:

1.android:singleLine="true"

2.android:ellipsize="marquee"

3.android:focusableInTouchMode=“true”

4.android:focusable=“true”

基本上使用自定義的MyMarqueeTextView 以及在佈局文件中設置了上面四個屬性之後,就能在多個view的佈局文件中正常的滾動了,如果不能,嘗試加上下面的幾個屬性:

1.android:scrollHorizontally=“true”
2. android:marqueeRepeatLimit=“marquee_forever”


再如果TextView放置在layout中,而layout也設置了android:focusable =“true”,那麼此時可以在layout中加個屬性addStatesFromChildren=“true",即父控件子控件都能獲得焦點。


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