android中怎么利用TextView實(shí)現(xiàn)跑馬燈效果-創(chuàng)新互聯(lián)

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

成都創(chuàng)新互聯(lián)公司是創(chuàng)新、創(chuàng)意、研發(fā)型一體的綜合型網(wǎng)站建設(shè)公司,自成立以來公司不斷探索創(chuàng)新,始終堅(jiān)持為客戶提供滿意周到的服務(wù),在本地打下了良好的口碑,在過去的十年時(shí)間我們累計(jì)服務(wù)了上千家以及全國政企客戶,如社區(qū)文化墻等企業(yè)單位,完善的項(xiàng)目管理流程,嚴(yán)格把控項(xiàng)目進(jìn)度與質(zhì)量監(jiān)控加上過硬的技術(shù)實(shí)力獲得客戶的一致表揚(yáng)。

一、要點(diǎn)

設(shè)置四個(gè)屬性

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="人生是一場(chǎng)無休、無歇、無情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無形的敵人作戰(zhàn)。" />

注意:singleLine屬性 不能換成 maxlLines

二、復(fù)雜布局

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

<?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="人生是一場(chǎng)無休、無歇、無情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無形的敵人作戰(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="人生是一場(chǎng)無休、無歇、無情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無形的敵人作戰(zhàn)。" />


</RelativeLayout>

這時(shí)候就需要自定義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="人生是一場(chǎng)無休、無歇、無情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無形的敵人作戰(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="人生是一場(chǎng)無休、無歇、無情的戰(zhàn)斗,凡是要做個(gè)夠得上稱為人的人,都得時(shí)時(shí)向無形的敵人作戰(zhàn)。" />


</RelativeLayout>

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

本文標(biāo)題:android中怎么利用TextView實(shí)現(xiàn)跑馬燈效果-創(chuàng)新互聯(lián)
文章鏈接:http://muchs.cn/article34/egppe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、App開發(fā)、企業(yè)網(wǎng)站制作、動(dòng)態(tài)網(wǎng)站、App設(shè)計(jì)品牌網(wǎng)站建設(shè)

廣告

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

外貿(mào)網(wǎng)站制作