自定义控件:滚动的TextView跑马灯

自定义滚动的textView的3中方法:

注意:TextView中的文字一定要充满一行,如果没有,那么是不会滚动的。

    方法一:xml中配置  
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"


    方法二:代码配置
TextView tv2 = (TextView) findViewById(R.id.tv2);
		tv2.setFocusable(true);
		tv2.setFocusableInTouchMode(true);


    方法三:继承重写

public class MyTextView extends TextView {

	//配置xml时调用
	public MyTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	//永远有焦点
	@Override
	@ExportedProperty(category = "focus")
	public boolean isFocused() {
		return true;
	}
}



注意:但是 由于默认的跑马灯效果是需要TextView的方法isFocused为true的时候才会跑动的,而默认时,只有一个TextView处于focused状态,只有方法三可以满足多个跑马灯的效果。


效果图:

第一个:方法一

第二个:方法二(注释掉第一个才有效果)

第三个:方法三

第四个:方法三



第一个:方法一:

    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:text="@string/text1"
        android:textColor="#ff0000"  />

第二个:方法二(注释掉第一个才有效果)

TextView tv2 = (TextView) findViewById(R.id.tv2);
		tv2.setFocusable(true);
		tv2.setFocusableInTouchMode(true);

第三个+第四个:方法三

public class MyTextView extends TextView {

	//配置xml时调用
	public MyTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	//永远有焦点
	@Override
	@ExportedProperty(category = "focus")
	public boolean isFocused() {
		return true;
	}
}
    <com.android.customtextview.MyTextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginTop="20dp"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="@string/text3"
        android:textColor="#ff0000" />

    <com.android.customtextview.MyTextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginTop="20dp"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="@string/text4"
        android:textColor="#ff0000" />
demo:http://download.csdn.net/detail/ss1168805219/9497141





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值