android 跑马灯效果

本文介绍了一个自定义的TextView类MyTextView,通过重写isFocused()方法使TextView默认获得焦点,适用于需要自动聚焦的场景。文章提供了详细的代码实现,并展示了如何在布局文件中使用。

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

public class MyTextView extends TextView {
    //在用代码创建的时候调用
    public MyTextView(Context context) {
        this(context, null);
    }

    //在识别XML的时候会调用此方法创建Textview,底层会用反射去AttribestSet去取属性值
    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    //给第一个构造函数和第二个使用
    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    //解决一个问题,需要Textview天生获取焦点
    @Override
    public boolean isFocused() {
        return true;
    }

}

布局里面直接调用即可

<top.test.test.MyTextView
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true"
        android:text="请输入你要输入的文字" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值