多个TextView跑马灯效果实现

来源:互联网 发布:java命令行参数args 编辑:程序博客网 时间:2024/05/01 18:48

显示跑马灯效果的前提条件就是你的文本内容要比显示文本的外部组件长,即外部组件无法完整的显示内部的文本内容
activity_main.xml
//两个TextView
  <com.example.textview.marqueeTextView     (原本的TextView改为包名.类名)
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:ellipsize="marquee"              
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true"                        
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <com.example.textview.marqueeTextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/textView2"
        android:text="@string/hello_world2" />

android:singleLine 表示使用单行文字
android:marqueeRepeatLimit,设置走马灯滚动的次数。
android:ellipsize,设置了文字过长时如何切断文字, 如果使用走马灯效果则设为marquee.
android:focusable,Android的缺省行为是在控件获得Focus时才会显示走马灯效果

新建一个marqueeTextView.java类,继承TextView类。添加TextView的三个构造函数,使用isFocused方法实现滚动。
marqueeTextView.java
public class marqueeTextView extends TextView{
public marqueeTextView(Context context) {
super(context);
}
public marqueeTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public marqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
 @Override
 public boolean isFocused() {
 return true;
   }
}

1 0
原创粉丝点击