2011年10月29日 星期六

如何讓TextView總是在捲動

首先,要讓一個TextView捲動只要設定以下style資訊即可:

    <style name="scrolling line">

        <item name="android:singleLine">true
        <item name="android:ellipsize">marquee
        <item name="android:marqueeRepeatLimit">marquee_forever
        <item name="android:scrollHorizontally">true
        <item name="android:focusable">true
        <item name="android:focusableInTouchMode">true
    </style>


可是這有個限制,就是要在focused的狀況下才會進行捲動。
那要怎麼讓他總是在捲動?很簡單,讓他永遠被focus就好了。
自訂一個類別繼承TextView,然後override以下方法:


@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;
}
這樣就可以讓他一直都在捲動了。

沒有留言:

張貼留言