Android利用TextView制作跑马灯该效果

android:singleLine="true"  强制文本只能单行显示,但会在最后显示省略号(···)

TextView省略文字显示
android:ellipsize="start"        省略号在开头        
android:ellipsize="middle"       省略号在中间        
android:ellipsize="end"          省略号在结尾        
android:ellipsize="marquee"      跑马灯显示(不显示省略号了,但还没跑起来)

android:focusable="true"          可聚焦
android:focusableInTouchMode="true"    添加这两行后可以初步实现效果

但只能实现一行时的效果,如果放两个TextView则只会第一个有效果,第二个不会动。

实现方法:
在MainActivity旁边新建一个类MarqueeTest继承TextView

public class MarqueeTest extends TextView {
    public MarqueeTest(Context context) {
        super(context);
    }

    public MarqueeTest(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MarqueeTest(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean isFocused(){
        return true; //判断是否处于显示状态,重写方法后让两个TextView都实现效果
    }
}


TextView类型修改:
    <com.bcp.paomadengtest.MarqueeTest
        android:id="@+id/text2"
        android:layout_below="@id/text"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="@string/str" />

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值