android中怎么利用TextView實(shí)現(xiàn)跑馬燈效果

這篇文章給大家介紹android中怎么利用TextView實(shí)現(xiàn)跑馬燈效果,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

成都創(chuàng)新互聯(lián)公司為客戶提供專業(yè)的成都做網(wǎng)站、成都網(wǎng)站設(shè)計、程序、域名、空間一條龍服務(wù),提供基于WEB的系統(tǒng)開發(fā). 服務(wù)項目涵蓋了網(wǎng)頁設(shè)計、網(wǎng)站程序開發(fā)、WEB系統(tǒng)開發(fā)、微信二次開發(fā)、手機(jī)網(wǎng)站制作等網(wǎng)站方面業(yè)務(wù)。

一、要點(diǎn)

設(shè)置四個屬性

android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"

直接在xml中使用

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:singleLine="true"
  android:ellipsize="marquee"
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:text="人生是一場無休、無歇、無情的戰(zhàn)斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰(zhàn)。" />

注意:singleLine屬性 不能換成 maxlLines 

二、復(fù)雜布局

在復(fù)雜的布局中可能不會實(shí)現(xiàn)跑馬燈效果。例如如下布局中,就只有第一個TextView會有跑馬燈效果

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/tv1"
  android:singleLine="true"
  android:ellipsize="marquee"
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:text="人生是一場無休、無歇、無情的戰(zhàn)斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰(zhàn)。" />
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@+id/tv1"
  android:layout_marginTop="10dp"
  android:singleLine="true"
  android:ellipsize="marquee"
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:text="人生是一場無休、無歇、無情的戰(zhàn)斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰(zhàn)。" />


</RelativeLayout>

這時候就需要自定義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;
 }
}

布局中使用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <com.example.dhj.marqueedemo.View.MarqueeText
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/tv1"
  android:singleLine="true"
  android:ellipsize="marquee"
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:text="人生是一場無休、無歇、無情的戰(zhàn)斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰(zhàn)。" />
 <com.example.dhj.marqueedemo.View.MarqueeText
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@+id/tv1"
  android:layout_marginTop="10dp"
  android:singleLine="true"
  android:ellipsize="marquee"
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:text="人生是一場無休、無歇、無情的戰(zhàn)斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰(zhàn)。" />


</RelativeLayout>

關(guān)于android中怎么利用TextView實(shí)現(xiàn)跑馬燈效果就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

當(dāng)前名稱:android中怎么利用TextView實(shí)現(xiàn)跑馬燈效果
網(wǎng)站URL:http://muchs.cn/article14/pphdge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、軟件開發(fā)、品牌網(wǎng)站設(shè)計企業(yè)網(wǎng)站制作、網(wǎng)站導(dǎo)航商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)