TextView跑马灯可以让显示不完的文字,以动画的形式滚动显示,但今天写了竟然没有任何效果这就很郁闷了!原来是没有获取到焦点
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="@string/android"
android:textColor="#9351e4"
android:textSize="24sp" />
这里必须要设置的几个属性
android:ellipsize="marquee"//文字显示不完全,以什么方式显示(这里就以滚动的行形式)
android:focusable="true"//获得焦点
android:focusableInTouchMode="true"//获得触摸焦点
android:marqueeRepeatLimit="marquee_forever"//滚动模式
android:scrollHorizontally="true"//横向滚动
android:singleLine="true"//以单行文本显示
//几个不同的值
android:ellipsize="start"//开头以...隐藏文字
middle//中间以...隐藏文字
end//结尾以...隐藏文字
显示的文字必须要超出给定的宽度,到这里如果不出意外就可以看到文字滚动了。
当你的界面view太多的时候,往往这个TextView就不一定能够获取到焦点,获取不到焦点也就看不懂跑马灯效果了下面给出解决办法
/**
*自定义TextView 重写isFocused()函数,让他放回true也就是一直获取了
*焦点效果自然也就出来了,如果这都不能解决那肯定就不是焦点问题了。
*那就要找到问题,在想办法解决
*/
public class MarqueTextView extends TextView {
public MarqueTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MarqueTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarqueTextView(Context context) {
super(context);
}
@Override
public boolean isFocused() {
return true;
}
}