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)無休、無歇、無情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無形的敵人作戰(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)無休、無歇、無情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無形的敵人作戰(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)無休、無歇、無情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無形的敵人作戰(zhàn)。" /> </RelativeLayout>
這時(shí)候就需要自定義View,實(shí)現(xiàn)跑馬燈效果
自定義MarQueeTextView extents TextView 重寫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)無休、無歇、無情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無形的敵人作戰(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)無休、無歇、無情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無形的敵人作戰(zhàn)。" /> </RelativeLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 開發(fā)之dataBinding與ListView及事件
這篇文章主要介紹了Android 開發(fā)之dataBinding與ListView及事件的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
Android 程序執(zhí)行Linux命令的解決方法及注意事項(xiàng)
這篇文章主要介紹了Android 程序執(zhí)行LINUX命令的解決方法及注意事項(xiàng),本文通過問題描述最終到解決方法,給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-12-12
Android編程實(shí)現(xiàn)的重力感應(yīng)示例代碼
這篇文章主要介紹了Android編程實(shí)現(xiàn)的重力感應(yīng)效果,以完整示例代碼形式分析了重力感應(yīng)的原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android DrawLayout結(jié)合ListView用法實(shí)例
這篇文章主要介紹了Android DrawLayout結(jié)合ListView用法實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Android程序開發(fā)之UIScrollerView里有兩個(gè)tableView
這篇文章主要介紹了UIScrollerView里有兩個(gè)tableView 的相關(guān)資料,需要的朋友可以參考下2016-04-04
解析Android開發(fā)優(yōu)化之:對(duì)Bitmap的內(nèi)存優(yōu)化詳解
在Android應(yīng)用里,最耗費(fèi)內(nèi)存的就是圖片資源。而且在Android系統(tǒng)中,讀取位圖Bitmap時(shí),分給虛擬機(jī)中的圖片的堆棧大小只有8M,如果超出了,就會(huì)出現(xiàn)OutOfMemory異常。所以,對(duì)于圖片的內(nèi)存優(yōu)化,是Android應(yīng)用開發(fā)中比較重要的內(nèi)容2013-05-05
使用Android Studio實(shí)現(xiàn)為系統(tǒng)級(jí)的app簽名
這篇文章主要介紹了使用Android Studio實(shí)現(xiàn)為系統(tǒng)級(jí)的app簽名,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03

