1.TextView中设置三个属性:
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
(可实现单个TextView的跑马灯效果)
2.重写TextView的isFocused()方法:
/*
*新建一个类继承TextView类,重载其构造方法,并重写isFocused()方法:
*/
public class QueeTextView extends TextView {
//构造函数1
public QueeTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
//构造函数2
public QueeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
//构造函数3
public QueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
//重写isFocued()方法,返回值始终为true
@Override
@ExportedProperty(category = "focus")
public boolean isFocused() {
return true;
}
}
3.xml文件中textView中引用新建的类:
<com.myandroid.queetext.QueeTextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/my_quee" />
<com.myandroid.queetext.QueeTextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="@string/my_quee2" />