TextView總是顯示跑馬燈效果

來源:互聯網
上載者:User

首先,使用TextView實現走馬燈形式的滾動顯示,只需要對其設定兩個屬性:

android:ellipsize="marquee"android:marqueeRepeatLimit="marquee_forever"

但是,TextView的滾動顯示,有一個前提,TextView需要必須處於focus狀態。當TextView失去焦點的時候,TextView將會停止滾動。如何?無限滾動,當然也需要從焦點入手。當然,直接requestFocus()是不行的,這裡我使用了另外一個方法。觀察到textView有一個名為isFocused()的方法,文檔中的注釋是這樣的:

/*** Returns true if this view has focus** @return True if this view has focus, false otherwise.*/

也就是說當TextView擁有焦點的時候會返回true.同時可以發現,TextView中很多地方都是直接調用這個方法作為判斷條件,最關鍵的,這個方法被聲明為public! ok, 實現方法已經初現端倪了!

 

做法是這樣:比如,現在寫一個類命名為一個AlwaysMarqueeTextView, 繼承自TextView, 同時override isFocused()方法,並使其傳回值為true, 範例如下:

 

 public class AlwaysMarqueeTextView extends TextView{<br />public AlwaysMarqueeTextView(Context context) {<br />super(context);<br />}</p><p>public AlwaysMarqueeTextView(Context context, AttributeSet attrs) {<br /> super(context, attrs);<br />}</p><p>public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle) {<br /> super(context, attrs, defStyle);<br />}</p><p>@Override<br />public boolean isFocused() {<br />return true;<br />}<br />}</p><p>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.