真正的TextView跑马灯

android自带的跑马灯是必须在有焦点的情况下才会滚动,一旦失去焦点跑马灯就会失去效果。

现在我的做法是自定义TextView 并重写isFocusedonFocusChanged方法,设置focused为true,这样TextView就可以始终获取到焦点

亲测可用!

1。自定义TextView:

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.ViewDebug.ExportedProperty;
import android.widget.TextView;

public class ScrollForeverTextView extends TextView
{

    public ScrollForeverTextView(Context context)
    {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public ScrollForeverTextView(Context context, AttributeSet attrs,
            int defStyle)
    {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public ScrollForeverTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    @Override
    @ExportedProperty(category = "focus")
    public boolean isFocused()
    {
        // TODO Auto-generated method stub
        return true;//重点
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction,
            Rect previouslyFocusedRect)
    {
        // TODO Auto-generated method stub
        super.onFocusChanged(true, direction, previouslyFocusedRect);//重点
    }
}



2。xml中引用:

           <。。。.view.ScrollForeverTextView
                android:id="@+id/title"
                style="@style/shadow5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:ellipsize="marquee"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:marqueeRepeatLimit="marquee_forever"
                android:maxEms="8"
                android:scrollHorizontally="true"
                android:singleLine="true"
                android:text=""
                android:textColor="@color/white"
                android:textSize="@dimen/text_size_18" />


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值