這篇文章給大家介紹android中怎么利用TextView實(shí)現(xiàn)跑馬燈效果,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
一、要點(diǎn)
設(shè)置四個(gè)屬性
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
直接在xml中使用
注意:singleLine屬性 不能換成 maxlLines
二、復(fù)雜布局
在復(fù)雜的布局中可能不會(huì)實(shí)現(xiàn)跑馬燈效果。例如如下布局中,就只有第一個(gè)TextView會(huì)有跑馬燈效果
這時(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; } }
布局中使用
關(guān)于android中怎么利用TextView實(shí)現(xiàn)跑馬燈效果就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。