解决TextView设置跑马灯但却没有效果

转载自:http://blog.csdn.net/a_zhon

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;
    }
}






  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值