TextView跑馬燈效果,textview跑馬燈

來源:互聯網
上載者:User

TextView跑馬燈效果,textview跑馬燈

轉載:http://www.2cto.com/kf/201409/330658.html

一、只想讓TextView顯示一行,但是文字超過TextView的長度怎麼辦?
在開頭顯示省略符號

android:singleLine="true" android:ellipsize="start"

在結尾顯示省略符號

android:singleLine="true" android:ellipsize="end"

在中間顯示省略符號

android:singleLine="true" android:ellipsize="middle"

橫向自動滾動(跑馬燈效果)

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

以上4個效果都要加上 android:singleLine="true",因為TextView預設是會自動換行的

android:ellipsize是設定文字過長時,該怎麼顯示

android:marqueeRepeatLimit="marquee_forever"是設定永遠重複,當然你也可以設定具體的數字

android:focusable="true"和android:focusableInTouchMode="true"一定要加上,不然滾動效果出不來

二、怎麼讓TextView可以垂直滾動?
在Java代碼中加入下面一句話就可以實現垂直滾動:textView.setMovementMethod(ScrollingMovementMethod.getInstance());

三、怎麼使TextView內容改變,跑馬燈效果依然可以使用

重寫TextView設定TextView一直處於選中狀態:AlwaysMarqueeTextView.java

/* * 重寫TextView保證跑馬燈效果一直顯示 */public class AlwaysMarqueeTextView extends TextView {    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;//一定要設定為true    }}

 

聯繫我們

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