使用TextView可以实现跑马灯效果

一般而言TextView内容长度太大,是会拆行显示的

布局限制时
使用android:singleLine="true"属性,可以只显示一行,但是不足无法显示部分带省略号。

使用android:ellipsize="marquee" 可以很好的解决这个问题,但仍然不会动

android:focusable="true"
android:focusableInTouchMode="true"//加上这两句属性,使TextView拿到页面焦点使其能够让文字循环移动

但是这种措施只针对有一个TextView要实现跑马灯的效果,因为其焦点只有一个,被第一拿了之后,

其他的就无法进行使用。


可以通过自定义一个MyTextView去继承TextView并改写

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

使自定义的所有对象都可以取得Fo'cused,就能都是实现跑马灯效果了。

当然要设置其在某个点执行跑马灯也是可以的,这就得在java代码中去设置了。


    <com.example.acher.textviewtext.MyTextView
        android:id="@+id/textview1"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        />
    <com.example.acher.textviewtext.MyTextView
        android:id="@+id/textview2"
        android:text="2222222222222222222222222222222222222222222222222222222222222222222"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        />

public class MyTextView extends TextView {
    public MyTextView(Context context) {
        super(context);
    }
    public MyTextView(Context context,AttributeSet attrs,int defStyle){
        super(context,attrs,defStyle);
    }
    public MyTextView(Context context,AttributeSet attrs)
    {
        super(context,attrs);
    }

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值