Android用過TextView實(shí)現(xiàn)跑馬燈效果的示例
以前就遇到過這個(gè)問題,今天重新拾起來。
跑馬燈效果其實(shí)就是當(dāng)文字超過TextView控件寬度的時(shí)候,使用滾動(dòng)的方式顯示出來:
方法1:(直接xml搞定)
Android系統(tǒng)中TextView實(shí)現(xiàn)跑馬燈效果,必須具備以下幾個(gè)條件:
1、android:ellipsize=”marquee”;
2、TextView必須單行顯示,且內(nèi)容必須超出TextView寬度;
3、TextView要獲得焦點(diǎn)才能滾動(dòng)。
xml代碼如下:
<TextView android:id="@+id/alarm_location" android:layout_width="20dp" android:padding="@dimen/space_4" android:layout_height="wrap_content" app:layout_rowWeight="2" app:layout_columnWeight="2" android:text="0" android:ellipsize="marquee" android:focusableInTouchMode="true" android:singleLine="true" android:focusable="true"/>
其中:ellipsize屬性指的是文字長(zhǎng)度超過TextView的長(zhǎng)度的時(shí)候的顯示方式,具體參數(shù)有
**Android:ellipsize=”start”—–省略號(hào)顯示在開頭 “…pedia”
android:ellipsize=”end”——省略號(hào)顯示在結(jié)尾 “encyc…”
android:ellipsize=”middle”—-省略號(hào)顯示在中間 “en…dia”
android:ellipsize=”marquee”–以橫向滾動(dòng)方式顯示(需獲得當(dāng)前焦點(diǎn)時(shí))**
方法2(自定義控件)
我看了自己以前的實(shí)現(xiàn)方式是這樣,也許在低版本的平臺(tái)上第一種方式適配不好吧。如果達(dá)不到要求可以試試這種方式。
TextVeiwSlide.Java
package edu.hrbeu.ice.mobilecar.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* @author 編寫人: xiaox
* @date 創(chuàng)建時(shí)間: 2017/1/10
* @Description 功能描述: 該類
*/
public class TextViewSlide extends TextView {
public TextViewSlide(Context context) {
super(context);
}
public TextViewSlide(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean isFocused() {
return true;
}
}
activity_main.xml
<edu.hrbeu.ice.mobilecar.widget.TextViewSlide android:id="@+id/alarm_type" android:layout_width="@dimen/item_width" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_gravity="center_vertical" android:layout_weight="1" android:padding="8dp" android:marqueeRepeatLimit="marquee_forever" android:ellipsize="marquee" android:scrollHorizontally="true" android:focusableInTouchMode="true" android:singleLine="true" tool:text="asda" />
可以看到第二種方式也就是在自定義控件中獲取了該控件的焦點(diǎn)。感覺和第一種沒有區(qū)別。
目前在android7.1.1和android4.4上實(shí)驗(yàn)兩種方法都沒有問題。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 指紋識(shí)別詳解及實(shí)現(xiàn)方法
本文主要介紹Android 指紋識(shí)別的知識(shí),這里整理了詳細(xì)的資料和簡(jiǎn)單實(shí)現(xiàn)代碼,有開發(fā)這部分的朋友可以參考下2016-09-09
Android實(shí)戰(zhàn)項(xiàng)目之實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器
隨著移動(dòng)互聯(lián)網(wǎng)的普及,手機(jī)應(yīng)用程序已經(jīng)成為人們生活中不可或缺的一部分,計(jì)算器是一類被廣泛使用的應(yīng)用程序之一,這篇文章主要給大家介紹了關(guān)于Android實(shí)戰(zhàn)項(xiàng)目之實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器的相關(guān)資料,需要的朋友可以參考下2023-10-10
Android網(wǎng)絡(luò)工具類NetworkUtils詳解
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)絡(luò)工具類NetworkUtils,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android App調(diào)用MediaRecorder實(shí)現(xiàn)錄音功能的實(shí)例
這篇文章主要介紹了Android App調(diào)用MediaRecorder實(shí)現(xiàn)錄音功能的實(shí)例,MediaRecorder非常強(qiáng)大,不僅能夠用來錄制音頻還可以錄制視頻,需要的朋友可以參考下2016-04-04
Android webview 遇到android.os.FileUriExposedException錯(cuò)誤解決辦法
這篇文章主要介紹了Android webview 遇到android.os.FileUriExposedException錯(cuò)誤解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家遇到這樣的問題解決,需要的朋友可以參考下2017-10-10
android之App Widget開發(fā)實(shí)例代碼解析
本篇文章主要介紹了App Widget框架的實(shí)例應(yīng)用,AppWidget就是我們平常在桌面上見到的那種一個(gè)個(gè)的小窗口,利用這個(gè)小窗口可以給用戶提供一些方便快捷的操作。有需要的可以了解一下。2016-11-11
Android FTP 多線程斷點(diǎn)續(xù)傳下載\上傳的實(shí)例
本篇文章主要介紹了Android FTP 多線程斷點(diǎn)續(xù)傳下載\上傳的實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08

