歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android TextView實現跑馬燈效果

Android TextView實現跑馬燈效果

日期:2017/3/1 9:43:58   编辑:Linux編程

研究下了讓TextView一行顯示數據,多的數據用省略號來表示,實現了,然後又繼續研究看怎麼樣能夠使TextView實現跑馬燈效果,這樣用戶可以完整的看到所有的數據。

在實際的開發中,我們有時候需要滾動的顯示信息,這就是我們所說的跑馬燈效果。Android中的TextView可以很容易的顯示這個效果,只需要添加以下屬性就可以了。

效果圖如下

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

但是這樣子有一個缺點,就是這種狀態的跑馬燈只能在TextView處於焦點狀態的時候,它才會滾動,對於實際的開發應用中很不實用,為了是跑馬燈無論在什麼情況下都能跑起來,這裡需要自定義一個TextView,它繼承TextView,並且重寫isFocuse()方法,讓它永遠返回true,這樣跑馬燈效果就能一直的跑起來了。

public class MarqueeTextView extends TextView {

public MarqueeTextView(Context context) {
super(context);
}

public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MarqueeTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}

@Override
public boolean isFocused() {
return true;
}

}

在xml中引用

<com.heynine.widget.view.MarqueeTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/marquee_text1" />

<com.heynine.widget.view.MarqueeTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/marquee_text2" />

這樣就可以顯示了

效果圖見上。

-------------------------------分割線-------------------------------

再來一例:

1.TextView一行顯示數據,多的數據用省略號來表示

<TextView android:layout_height="wrap_content"
android:layout_width="200dip"
android:id="@+id/textView1"
android:text="" android:singleLine="true"
android:ellipsize="end"
>

2.TextView實現跑馬燈效果

<TextView android:layout_height="wrap_content"
android:layout_width="200dip"
android:id="@+id/textView1"
android:text="" android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusableInTouchMode="true"
android:focusable="true">

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved