亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Android:不一樣的TextView(一)水平滾動-跑馬燈

標簽:
Android

引语:如果你已经尽了最大的努力,就不要为任何的失败而气馁;若没有,就不要找任何借口。

说到实现TextView文本水平滚动,大多数人首先会想到的是跑马灯,跑马灯实现起来比较容易,可以在布局文件中设置TextView属性实现。如:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ellipsize="marquee"

android:focusable="true"

android:focusableInTouchMode="true"

android:singleLine="true"

android:text="@string/marquee_content" />

注意要实现跑马灯效果必须要有三要素,否则无法实现:

1、跑马灯特效声明

android:ellipsize="marquee"

2、焦点

android:focusable="true"

android:focusableInTouchMode="true"

3、单行显示

android:singleLine="true"

除了这三要素之外,还有一个原则。

TextView上的文本长度要超过TextView的宽度,否则也无法实现跑马灯效果。

除了以上几个属性设置以外,还有几个额外的属性可以进行设置,如果不设置不影响跑马灯效果。

1、android:marqueeRepeatLimit="marquee_forever"// 一直循环跑(可以换成相应数字此时对应-1)。

2、android:scrollHorizontally="true"// 水平滚动。

这里存在一些弊端:如,在Android SDK高版本中,文本单行显示singleLine="true"属性过时,所以要使用maxLines="1"来代替,但是如果将singleLine="true"替换成maxLines="1",跑马灯将会失效。除此之外跑马灯特效还受焦点限制,当TextView失去焦点时,跑马灯也会失效。

对于焦点问题,可以采用重写TextView中的isFocused方法来实现。如:

public class MarqueeTextView extends android.support.v7.widget.AppCompatTextView {
public MarqueeTextView(Context context) {
super(context);
}
public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
// 焦点
@Override
public boolean isFocused() {
return true;
}
}

使用,在XML布局文件中添加如下代码即可:

<cc.ibooker.rtextview.MarqueeTextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ellipsize="marquee"

android:marqueeRepeatLimit="marquee_forever"

android:singleLine="true"

android:text="@string/marquee_content" />

在实际的生产开发当中,往往不在满足一条文本的跑马灯,可以存在多条文本实现跑马灯,水平或者垂直滚动。如一些商城APP当中,热门推荐,积分抽奖通知等等,则采用垂直滚动的方式,那么如何实现尼,我会在下篇文章中进行讲解。

Github:RTextView

5b9d2aac0001a28402160238.jpg

原文链接:http://www.apkbus.com/blog-613740-68015.html

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消