android 文字超出控制元件寬度時,自動滾動顯示,類似跑馬燈效果
阿新 • • 發佈:2019-01-19
1.自定義控制元件
自定義RollTextView 繼承TextView,重寫 isFocused(),返回為ture 即可;
public class RollTextView extends TextView {
public RollTextView (Context context) {
super(context);
}
public RollTextView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public RollTextView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean isFocused() {
return true;
}
}
2.佈局檔案:
<com.ss.demo.RollTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="這是個TextView自動滾動效果示意案列,實現單行滾動效果"
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
android:scrollHorizontally="true"
android:background="@drawable/bg_fff_25"
android:textSize="20sp"
android:textColor="#222"
android:padding="5dp"/>
eandroid:ellipsize=”marquee”設定可以以橫向滾動方式顯示,但前提是需獲得當前焦點,所以才有的上一步,重寫 isFocused(),返回為ture則獲取焦點;
android:marqueeRepeatLimit=”marquee_forever”設定跑馬燈顯示次數,marquee_forever表示不間斷無限次,也可以通過程式碼textview.setMarqueeRepeatLimit(1)設定次數(設定-1 則是無限滾動);
android:scrollHorizontally=”true”設定水平顯示;
3.效果圖:
當顯示的文字超出控制元件的寬度時,則自動滾動顯示,不超過,則不滾動;