慕课网--用textview实现跑马灯效果

当TextView中的文字很长,但又想一行显示完的时候,可以用跑马灯的效果

TextView的属性中可以如下设置:

    <com.example.test_textview.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusableInTouchMode="true"
        android:focusable="true"
        android:text="@string/hello_world" />

一个textView创建出来,如果里面的文字很长,默认是这样:

设置singleLine为true后显示为这样:

ellipsize设置为marquee,focusable和focusableInTouchMode设置为true后,可以得到跑马灯效果:

但是如果在下面再添加一个同样的textView,会发现它不能达到效果,是因为只有第一个textView是focus的。

如果再添加一个EditText,会发现两个第一个TextView也不能动了,因为会默认focus在EditText上面。

可以新定义一个类,继承TextView,重写focus函数,将需要跑马灯效果的TextView focus上。


    <com.example.test_textview.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="@string/hello_world" />
    <com.example.test_textview.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="@string/hello_world" />



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值