Android中實現乙個跑馬燈的效果

2021-06-24 11:09:30 字數 1447 閱讀 1513

android中實現乙個跑馬燈的效果:

在新建乙個android product之後,在布局檔案main中替換掉原來的textview

**如下:

android:layout_width="60dp"

android:layout_height="wrap_content"

android:textcolor="@android:color/black" 

android:ellipsize="marquee" 

android:focusable="true" 

android:focusableintouchmode="true"

android:marqueerepeatlimit="marquee_forever" 

android:scrollhorizontally="true"

android:singleline="true"

android:text="這是跑馬燈的效果這是跑馬燈的效果">

其他部分**不用改動,在模擬器上應該可以看到滾動的顯示"這是跑馬燈的效果這是跑馬燈的效果"。

其中有幾個問題是我自己碰到的,列舉如下:

1.寬度android:layout_width不可以設定為wrap_content(自適應內容)

2.android:text最好是比較長的字串,最少要長過自己設定的width大小(我這裡是60dp)

3.顏色最好別設定為@android:color/white,否則你什麼都看不到,因為背景顏色也是white

4.在xml中,textview 的屬性

android:ellipsize = "end"    省略號在結尾

android:ellipsize = "start"   省略號在開頭

android:ellipsize = "middle"     省略號在中間

android:ellipsize = "marquee"  跑馬燈

5.android:scrollhorizontally="true"相信大家英文水平還不錯的話都明白這句的意思是水平滾動

6.android:marqueerepeatlimit="marquee_forever" 看英文同樣可以明白,是無限次迴圈,學習android,英文基礎還是必須的。

7.最後還有兩句**沒有解釋:

android:focusable="true" //讓textview獲得焦點

android:focusableintouchmode="true" //針對觸控螢幕獲得當前焦點

事實上這兩句**我也了解不是特別多,在網上查了下這兩個屬性,目前理解為:

android:focusable="true"相當於當前activity開啟的時候,讓當前控制項textview獲得焦點,才可以實現滾動功能

android:focusableintouchmode="true"和上述屬性應該是類似的,但限制應該是指的觸控螢幕上的意思

Android之跑馬燈

不好直接上專案效果圖所以就做了乙個簡單的demo放手機上顯示效果了。做起來也很簡單,在布局檔案裡面設定幾個屬性就ok.先上效果圖 設定如下。設定為跑馬燈顯示 android ellipsize marquee 獲取焦點 android focusable true 可以通過toucth來獲得focu...

簡單實現跑馬燈

1.可以自己實現自定義view public class marqueetestview extends textview implements runnable 畫筆工具 override protected void ondraw canvas canvas override public vo...

jQuery實現跑馬燈

前段時間做專案乙個需求,輪播方式實現不了,自己寫了乙個跑馬燈實現效果。html class container class scroll div class scroll begin href 文字1a li href 文字2a li href 文字3a li href 文字4a li href 文...