在开发的过程中,有时候界面一定不能完全显示出文本,这个时候我们就可以使用到跑马灯了。
我们TextVeiw有属性可以设置跑马灯。
例如:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你好的dskajfkajslkdjfjaksldjfkjksdjalfjaskldfjlasdf "
android:singleLine="true" //设置单行显示
android:ellipsize="marquee" //设置跑马灯的属性
android:focusable="true" //获取焦点
android:focusableInTouchMode="true" //在触摸的设备上获取焦点
/>
注意:我们要将 focusable focusableInTouchMode 都设置为true
这个时候我们发现界面上只能有一个文本有跑马灯的效果。我们想要多个文本都跑起来。
有个解决方案:就是我们自己写一个TextView,让这个TextView强制获取焦点就行啦。
1、创建一个类:
创建一个MyTextView ,继承TextView
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context) {
super(context);
}
//我们重写这个方法,默认一直返回true,这样系统底层在判断是否有焦点的时候就一直是true
@Override
public boolean isFocused() {
return true;
}
}
2、在xml文件中使用:
我们自己定义的View要写全包名,现在我们只有设置 singleLine ellipsize 这2个属性就可以了。
<com.example.testv7.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你好的dskajfkajslkdjfjaksldjfkjksdjalfjaskldfjlasdf "
android:singleLine="true"
android:ellipsize="marquee"
/>
<com.example.testv7.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你好的dskajfkajslkdjfjaksldjfkjksdjalfjaskldfjlasdf "
android:singleLine="true"
android:ellipsize="marquee"
/>
亲自测试OK,界面多个文本同时跑起来了