Android中TextView控件的跑马灯效果实现

1.TextView中设置三个属性:

 android:singleLine="true"
 android:ellipsize="marquee"
 android:focusable="true"
 android:focusableInTouchMode="true"

(可实现单个TextView的跑马灯效果)

2.重写TextView的isFocused()方法:

/*
*新建一个类继承TextView类,重载其构造方法,并重写isFocused()方法:
*/

public class QueeTextView extends TextView {
    //构造函数1
    public QueeTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    //构造函数2
    public QueeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
    //构造函数3
    public QueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    //重写isFocued()方法,返回值始终为true
    @Override
    @ExportedProperty(category = "focus")
    public boolean isFocused() {
        return true;
    }
}

3.xml文件中textView中引用新建的类:

 <com.myandroid.queetext.QueeTextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:text="@string/my_quee" />

<com.myandroid.queetext.QueeTextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:singleLine="true"
    android:text="@string/my_quee2" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值