TextView实现跑马灯效果

关键代码:

1、android:singleLine="true"     控制内容单行显示

2、android:ellipsize="marquee"     跑马灯的形式显示内容

3、android:focusable="true"    允许获取焦点

4、android:focusableInTouchMode="true"     可以通过touch获取焦点

一、在values—>strings中设置要显示的内容,如:

     <string name="hello_world">学习android的过程是痛苦的,但坚持下去,终会有所收获。坚持!!加油! ! !</string>
<string name="marquee">通知:今天由于天气原因,放假一天,不再考勤、上课。明天是否上课会根据天气情况
另行通知。校团委宣。</string>

二、layout布局:

 

<span style="font-size:18px;"><com.example.marquee.MarqueeTest// 把TtextView改为自定义控件(包名.自定控义类名)
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"        
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="@string/hello_world"
        android:textSize="20sp"
        android:textColor="#00ff00" />

     <com.example.marquee.MarqueeTest // 把TtextView改为自定义控件(包名.自定控义类名)
        android:layout_below="@id/textview1"
        android:layout_marginTop="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="@string/marquee"
        android:textSize="20sp"
        android:textColor="#ff0000" /></span>

三、自定义一个类来继承TextView,在这个自定义类中要创建三个构造函数和实现isFocused()方法。

<span style="font-size:18px;"><span style="font-size:18px;">public class MarqueeTest extends TextView {

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

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

	public MarqueeTest(Context context) {
		super(context);
	}

	@Override
	public boolean isFocused() {
		return true;
	}
}</span></span>

快速创建构造函数的方法:右击——source——Generate Constructors from Superclass.....
     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值