這篇文章給大家介紹android中怎么利用TextView實現(xiàn)跑馬燈效果,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

一、要點
設(shè)置四個屬性
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="人生是一場無休、無歇、無情的戰(zhàn)斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰(zhàn)。" />
注意:singleLine屬性 不能換成 maxlLines
二、復雜布局
在復雜的布局中可能不會實現(xiàn)跑馬燈效果。例如如下布局中,就只有第一個TextView會有跑馬燈效果
<?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="人生是一場無休、無歇、無情的戰(zhàn)斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰(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="人生是一場無休、無歇、無情的戰(zhàn)斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰(zhàn)。" /> </RelativeLayout>
這時候就需要自定義View,實現(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="人生是一場無休、無歇、無情的戰(zhàn)斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰(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="人生是一場無休、無歇、無情的戰(zhàn)斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰(zhàn)。" /> </RelativeLayout>
關(guān)于android中怎么利用TextView實現(xiàn)跑馬燈效果就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
本文標題:android中怎么利用TextView實現(xiàn)跑馬燈效果-創(chuàng)新互聯(lián)
文章鏈接:http://chinadenli.net/article34/egppe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導航、App開發(fā)、企業(yè)網(wǎng)站制作、動態(tài)網(wǎng)站、App設(shè)計、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)