您的位置:首页 > 其它

使用TextView实现跑马灯效果

2016-08-01 10:11 399 查看

使用TextView实现跑马灯效果

分为几个步骤,

一、首先设置layout文件

<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="@string/hello_world"/>


加入singleLine,ellipsize = marquee,focusable,focusableInTouchMode = “true”。这样就可以实现了跑马灯效果

随后再加入一个TextView后,只有第一个TextView存在跑马灯效果,所以要用Focu强制转一下

方法如下:

二、新建一个class文件,继承TextView,加入其中三个构造器。

随后复写一个isFocused()。

public class MarqueeTest extends TextView {
public MarqueeTest(Context context) {
super(context);
}

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

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

@Override
public boolean isFocused() {
return true;
}
}


最后一把,在Layout中,将TextView的
<TextView
———>替换为:
<com.yourname.clicklinstener.MarqueeTest

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: