android跑马灯效果实现(一直滚动不受影响)

在android开发过程中需要用到跑马灯的字体,单实现该功能只需对TextView作如下配置:

    <TextView
        android:id="@+id/tv_main_scollText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/item_header"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:padding="3dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="@string/top_msg"
        android:textSize="17sp" />
但是在TextView失去焦点后跑马灯效果也就没有了,于是可以添加如下改进:

step1:写一个类继承自TextView,重写其isFocused()和onFocusChanged()方法:

	@Override
	public boolean isFocused() {
		return true;
	}

	@Override
	protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
	}

step2:在布局文件里引用你写的类:

    <com.zyt.widget.MyScollTextView
        android:id="@+id/tv_main_scollText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/item_header"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:padding="3dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="@string/top_msg"
        android:textSize="17sp" />

现在TextView已经一直获得焦点从而一直滚动,但是问题又来了:如果调用了其他功能,比如说EditText输入,则焦点就转到输入框了,外加键盘也被调出来,TextView又停止了滚动!因此需要再进一步:

step3:在刚写的类里重写onWindowFocusChanged()方法:

	@Override
	public void onWindowFocusChanged(boolean hasWindowFocus) {
		
	}
于是就大功告成了!博主亲测有效。

转载请注明~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值