android應用中用TextView實現跑馬燈效果執行個體教程

來源:互聯網
上載者:User

如何?兩個TextView的跑馬燈效果,如果使用常規的做法,只能夠使一個TextView起作用。 現在方法具體如下:

1.為TextView增加四個屬性

android:ellipsize="marquee"android:focusable="true"android:focusableInTouchMode="true"android:singleLine="true"


實現TextView類,實現三個建構函式並重載 isFocused方法。

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 defStyle) {     super(context, attrs, defStyle); } @Override public boolean isFocused() {     return true; }  }



在main.xml檔案中使用自己實現的TextView類。

這樣就實現了兩個跑馬燈的效果了。 這裡主要是重寫了isFocuse方法,這樣預設兩個TextView都被Focuse了,所以這兩個TextView都可以跑馬燈了。



Android實現多個TextView同時顯示跑馬燈效果

如果在一個較小的android頁面中,需要顯示較長的文案時無法顯示完全,於是很自然地想到了TextView中的marquee —— 跑馬燈效果,可是Android執行跑馬燈效果需要控制項擷取焦點,當某一控制項requestFocus()時,會將其他控制項的焦點搶去,這就導致了同一時間只能有一個控制項擷取焦點。那麼,如果讓多個控制項同時擷取並持有焦點呢?或者說,“欺騙”Android系統,讓它以為多個控制項都持有焦點,即每個控制項都在焦點狀態。

通過上述分析,問題轉化為如何讓多個控制項同時處於焦點狀態,這就需要重寫TextView的部分方法,達到“欺騙”Android系統的目的,這樣每個控制項requestFocus()之後,均讓自己處於焦點狀態,並且不可被剝奪焦點,就可以達到多個控制項同時“持有”焦點了,代碼如下:

public class MarqueeTextView extends TextView{    public MarqueeTextView(Context context)    {        this(context, null);    }        public MarqueeTextView(Context context, AttributeSet attrs)    {        super(context, attrs);                setFocusable(true);        setFocusableInTouchMode(true);                setSingleLine();        setEllipsize(TextUtils.TruncateAt.MARQUEE);        setMarqueeRepeatLimit(-1);    }        public MarqueeTextView(Context context, AttributeSet attrs, int defStyle)    {        super(context, attrs, defStyle);                setFocusable(true);        setFocusableInTouchMode(true);                setSingleLine();        setEllipsize(TextUtils.TruncateAt.MARQUEE);        setMarqueeRepeatLimit(-1);    }    @Override    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)    {        if (focused)        {            super.onFocusChanged(focused, direction, previouslyFocusedRect);        }    }        @Override    public void onWindowFocusChanged(boolean focused)    {        if (focused)        {            super.onWindowFocusChanged(focused);        }    }        @Override    public boolean isFocused()    {        return true;    }}




由於常規執行跑馬燈的屬性:

android:focusable="true"、android:focusableInTouchMode="true"、android:singleLine="true"、android:ellipsize="marquee"

在代碼中均已設定,故引用此控制項時,無需添加上述4個屬性。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.