在android开发过程中需要用到跑马灯的字体,单实现该功能只需对TextView作如下配置:
<TextView
android:id="@+id/tv_main_scollText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/item_header"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:padding="3dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="@string/top_msg"
android:textSize="17sp" />
但是在TextView失去焦点后跑马灯效果也就没有了,于是可以添加如下改进:
step1:写一个类继承自TextView,重写其isFocused()和onFocusChanged()方法:
@Override
public boolean isFocused() {
return true;
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
}
step2:在布局文件里引用你写的类:
<com.zyt.widget.MyScollTextView
android:id="@+id/tv_main_scollText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/item_header"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:padding="3dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="@string/top_msg"
android:textSize="17sp" />
现在TextView已经一直获得焦点从而一直滚动,但是问题又来了:如果调用了其他功能,比如说EditText输入,则焦点就转到输入框了,外加键盘也被调出来,TextView又停止了滚动!因此需要再进一步:
step3:在刚写的类里重写onWindowFocusChanged()方法:
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
}
于是就大功告成了!博主亲测有效。
转载请注明~