android TextView實(shí)現(xiàn)跑馬燈效果
本文實(shí)例為大家分享了android TextView跑馬燈效果的具體代碼,供大家參考,具體內(nèi)容如下
一、要點(diǎn)
設(shè)置四個(gè)屬性
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
直接在xml中使用
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:text="人生是一場(chǎng)無(wú)休、無(wú)歇、無(wú)情的戰(zhàn)斗,凡是要做個(gè)夠得上稱(chēng)為人的人,都得時(shí)時(shí)向無(wú)形的敵人作戰(zhàn)。" />
注意:singleLine屬性 不能換成 maxlLines
二、復(fù)雜布局
在復(fù)雜的布局中可能不會(huì)實(shí)現(xiàn)跑馬燈效果。例如如下布局中,就只有第一個(gè)TextView會(huì)有跑馬燈效果
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv1" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:text="人生是一場(chǎng)無(wú)休、無(wú)歇、無(wú)情的戰(zhàn)斗,凡是要做個(gè)夠得上稱(chēng)為人的人,都得時(shí)時(shí)向無(wú)形的敵人作戰(zhàn)。" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv1" android:layout_marginTop="10dp" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:text="人生是一場(chǎng)無(wú)休、無(wú)歇、無(wú)情的戰(zhàn)斗,凡是要做個(gè)夠得上稱(chēng)為人的人,都得時(shí)時(shí)向無(wú)形的敵人作戰(zhàn)。" /> </RelativeLayout>
這時(shí)候就需要自定義View,實(shí)現(xiàn)跑馬燈效果
自定義MarQueeTextView extents TextView 重寫(xiě)isFocused()方法,返回true
public class MarqueeText extends TextView { public MarqueeText(Context context) { super(context); } public MarqueeText(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MarqueeText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean isFocused() { return true; } }
布局中使用
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.dhj.marqueedemo.View.MarqueeText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv1" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:text="人生是一場(chǎng)無(wú)休、無(wú)歇、無(wú)情的戰(zhàn)斗,凡是要做個(gè)夠得上稱(chēng)為人的人,都得時(shí)時(shí)向無(wú)形的敵人作戰(zhàn)。" /> <com.example.dhj.marqueedemo.View.MarqueeText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv1" android:layout_marginTop="10dp" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:text="人生是一場(chǎng)無(wú)休、無(wú)歇、無(wú)情的戰(zhàn)斗,凡是要做個(gè)夠得上稱(chēng)為人的人,都得時(shí)時(shí)向無(wú)形的敵人作戰(zhàn)。" /> </RelativeLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 實(shí)現(xiàn)圓角圖片的簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)圓角圖片的簡(jiǎn)單實(shí)例的相關(guān)資料,Android 圓角圖片的實(shí)現(xiàn)形式,包括用第三方、也有系統(tǒng),需要的朋友可以參考下2017-07-07Android自定義View實(shí)現(xiàn)圓弧進(jìn)度效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)圓弧進(jìn)度效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11react native中的聊天氣泡及timer封裝成的發(fā)送驗(yàn)證碼倒計(jì)時(shí)
這篇文章主要介紹了react native中的聊天氣泡及timer封裝成的發(fā)送驗(yàn)證碼倒計(jì)時(shí)的相關(guān)資料,需要的朋友可以參考下2017-08-08Android JNI處理圖片實(shí)現(xiàn)黑白濾鏡的方法
這篇文章主要介紹了Android JNI處理圖片實(shí)現(xiàn)黑白濾鏡的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01android自定義波浪加載動(dòng)畫(huà)的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了android自定義波浪加載動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android視頻播放器屏幕左側(cè)邊隨手指上下滑動(dòng)亮度調(diào)節(jié)功能的原理實(shí)現(xiàn)
這篇文章主要介紹了Android視頻播放器屏幕左側(cè)邊隨手指上下滑動(dòng)亮度調(diào)節(jié)功能的原理實(shí)現(xiàn),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02Android實(shí)現(xiàn)系統(tǒng)重新啟動(dòng)的功能
有些Android版本沒(méi)有系統(tǒng)重啟的功能,非常不方便。需要我們自己開(kāi)發(fā)一個(gè)能夠重新啟動(dòng)的應(yīng)用2013-11-11Android使用Kotlin實(shí)現(xiàn)多節(jié)點(diǎn)進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android使用Kotlin實(shí)現(xiàn)多節(jié)點(diǎn)進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03Android ListView列表控件的介紹和性能優(yōu)化
這篇文章主要介紹了Android ListView列表控件的介紹和性能優(yōu)化,需要的朋友可以參考下2017-06-06Android網(wǎng)格布局GridView實(shí)現(xiàn)漂亮的多選效果
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)格布局GridView實(shí)現(xiàn)漂亮的多選效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12