關於安卓跑馬燈效果實現的課題研究

2021-07-11 11:13:26 字數 1295 閱讀 3840

關於安卓跑馬燈效果的課題研究:

1.對於這一效果的實現,只有在textview這一控制項中實現。而這一實現需要改寫xml檔案。而這一xml檔案存在於android專案res目錄下的。

關於這一效果的實現,在textview中有乙個屬性是android:ellipsize這一屬性,給定其乙個值為marquee的值,意為滾動效果。

然而這個時候就需要考慮乙個很重要的因素,那就是,既然是跑馬燈效果,那麼它是會滾動的,而這個滾動的次數就成了問題,解決這個問題就需要textview控制項中的屬性

android:marqueerepeatlimit這一屬性,給其設定的值為forever_marquee,設定為無限次數滾動。

然而,在安卓中它會根據你輸入的文字以及裝置的解析度問題,當你 的文字個數到達一定數量是就會自動的換行,這樣對於跑馬燈的效果則就沒有了意義,解決這一問題,用到了textview這一控制項中的android:scrollhorizontally這一屬性,將其設定為true。

2, 由於其跑馬燈的實現需要單行的滾動,則這裡就需要其textview中的android:singleline這一屬性,設定其為true。

這時,textview就具備了跑馬燈的效果,是不是感覺很簡單呢。但是接下來問題又出現了,就是當這一安卓程式執行在虛擬機器上發現並沒有出現跑馬燈的效果,這個又是為什麼呢?

這是因為,跑馬燈效果需要textview獲取當前的焦點,然而對於這一控制項來說,它預設其中clickable,longclickable,focusable,focusablelntouchmode這四個屬性都是false,所以跑馬燈效果因為獲取不到焦點的問題使其不能實現。

解決這個問題則需要將,focusable,focusablelntouchmode這兩個屬性的值修改為true,這樣一來解決了textview獲取焦點這乙個問題。從而使其跑馬燈效果完美展示。

具體的**如下:

android:id="@+id/tv"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ellipsize="marquee"

android:marqueerepeatlimt="forever_marquee"

android:singline="true"

android:hcrollhorizontally="true"

android:text="跑馬燈效果展示"

android:textcolor="#ff0000"

android:textsize="20dp"

/>

安卓跑馬燈效果

android layout width fill parent android layout height wrap content android singleline true android textsize 15sp android focusable true android text ...

安卓TextView滾動字幕實現跑馬燈

ui設計 mainactivity package com.example.android textview5 import android.os.bundle import android.text.html import android.text.method.linkmovementmetho...

使用TextView實現跑馬燈的效果

1 定義textview標籤的4個屬性 android singleline true 使其只能單行android ellipsize marquee 去掉省略號 android focusable true 使其迴圈 android focusableintouchmode true 為了永久的滾...