自定义的TextView跑马灯

本文介绍如何通过自定义TextView实现类似Button的滚动效果,并详细解释了实现步骤与关键代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先我们在Xml中定义一个TextView的控件
    ellipsize=start为开始为...
    ellipsize=end为最后...
    ellipsize=marquee为以跑马灯的形式

    <TextView
        Android:ellipsize="start"     首先我们在定义ellipsize时为start也不会在前面显示.....只有加上只有一行的属性singleLine为true才可以显示
        android:singleLine="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="你需要的版本立即升级 尽请期待中!!!下载送更多的礼品,你需要的版本立即升级 尽请期待中!!!下载送更多的礼品"
        />
改进如果我们把它改为Button控件(Button控件天生就有滚动的属性)

    <Button
    android:focusableInTouchMode="true" 设置成Button的滚动属性
        android:ellipsize="start"     
        android:singleLine="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="你需要的版本立即升级 尽请期待中!!!下载送更多的礼品,你需要的版本立即升级 尽请期待中!!!下载送更多的礼品"
        />
可是我们的需求不是Button按钮的效果 (这时候我们想把TextView天上下来就有Button属性的自定滚动)
这时候我们就开始创建自定义TextVeiw的包com.bawei.ui  实体类为FocusedTextView 然后继承TextView的类
实现我们TextView中的三个方法
    /
    *自定义一个TextView的
    让他自身有自己的焦点
    */
    public FocusedTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
    }
    
    
    public FocusedTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    }
    
    
    public FocusedTextView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    }
然后我们在创建出来存在焦点的方法
    @Override
    @ExportedProperty(category = "focus")
    public boolean isFocused() {
    // TODO Auto-generated method stub
    在isFocused中的父控件的源码标记判断是否有焦点 如果有焦点就返回true
    这时候我们不管他有没有焦点都让他返回true       return true;
    return super.isFocused();
    }
然后在我们FocusedTextView中 Copy Qualified Name把它放到TextView的控件中
     <com.example.TextSchool.Ui.FocusedTextView
        android:ellipsize="marquee"
        android:singleLine="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="你需要的版本立即升级 尽请期待中!!!下载送更多的礼品,你需要的版本立即升级 尽请期待中!!!下载送更多的礼品"
        />
    这样就完成了我们的自定义我们TextView的滚动
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值