android中怎么利用TextView實現跑馬燈效果

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

成都創新互聯公司為客戶提供專業的成都做網站、成都網站設計、程序、域名、空間一條龍服務,提供基于WEB的系統開發. 服務項目涵蓋了網頁設計、網站程序開發、WEB系統開發、微信二次開發、手機網站制作等網站方面業務。

一、要點

設置四個屬性

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="人生是一場無休、無歇、無情的戰斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰。" />

注意:singleLine屬性 不能換成 maxlLines 

二、復雜布局

在復雜的布局中可能不會實現跑馬燈效果。例如如下布局中,就只有第一個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="人生是一場無休、無歇、無情的戰斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰。" />
 <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="人生是一場無休、無歇、無情的戰斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰。" />


</RelativeLayout>

這時候就需要自定義View,實現跑馬燈效果

自定義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="人生是一場無休、無歇、無情的戰斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰。" />
 <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="人生是一場無休、無歇、無情的戰斗,凡是要做個夠得上稱為人的人,都得時時向無形的敵人作戰。" />


</RelativeLayout>

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

當前名稱:android中怎么利用TextView實現跑馬燈效果
網站URL:http://m.kartarina.com/article14/pphdge.html

成都網站建設公司_創新互聯,為您提供微信小程序軟件開發、品牌網站設計、企業網站制作、網站導航、商城網站

廣告

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

商城網站建設
主站蜘蛛池模板: 精品亚洲成α人无码成α在线观看 | 熟妇人妻系列av无码一区二区| 人妻少妇精品无码专区动漫| 无码喷水一区二区浪潮AV| 日韩精品无码Av一区二区| 亚洲中久无码永久在线观看同| 无码八A片人妻少妇久久| 日韩AV无码精品一二三区| 少妇无码一区二区三区免费| 无码任你躁久久久久久久| 亚洲精品无码专区| 无码人妻一区二区三区免费n鬼沢 无码人妻一区二区三区免费看 | 精品无码人妻一区二区免费蜜桃| 久久久久久久久免费看无码| 无码精品一区二区三区在线| 亚洲无码日韩精品第一页| 日本精品人妻无码免费大全 | 亚洲av无码一区二区三区天堂| 亚洲中文久久精品无码| 国产福利电影一区二区三区久久老子无码午夜伦不 | 无码精品人妻一区二区三区人妻斩 | 人妻少妇乱子伦无码视频专区 | 国产精品无码无片在线观看| 无码色AV一二区在线播放| 亚洲AV无码国产剧情| 麻豆国产精品无码视频| 无码日韩精品一区二区三区免费| 国产av永久无码天堂影院| 国产AV无码专区亚洲AV手机麻豆| 无码AV动漫精品一区二区免费| 久久亚洲精品无码gv| 亚洲日韩一区二区一无码| 人妻系列无码专区无码中出| 亚洲av日韩av无码| 亚洲欧洲自拍拍偷午夜色无码| 国产AV无码专区亚洲AV毛网站| 亚洲国产精品无码AAA片| 亚洲av无码一区二区乱子伦as| 国产日韩精品中文字无码| 丰满熟妇乱又伦在线无码视频| 国产AV无码专区亚洲AV男同|