android自带的跑马灯是必须在有焦点的情况下才会滚动,一旦失去焦点跑马灯就会失去效果。
现在我的做法是自定义TextView 并重写isFocused和onFocusChanged方法,设置focused为true,这样TextView就可以始终获取到焦点
亲测可用!
1。自定义TextView:
import android.content.Context; import android.graphics.Rect; import android.util.AttributeSet; import android.view.ViewDebug.ExportedProperty; import android.widget.TextView; public class ScrollForeverTextView extends TextView { public ScrollForeverTextView(Context context) { super(context); // TODO Auto-generated constructor stub } public ScrollForeverTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public ScrollForeverTextView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override @ExportedProperty(category = "focus") public boolean isFocused() { // TODO Auto-generated method stub return true;//重点 } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { // TODO Auto-generated method stub super.onFocusChanged(true, direction, previouslyFocusedRect);//重点 } }
2。xml中引用:
<。。。.view.ScrollForeverTextView android:id="@+id/title" style="@style/shadow5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:maxEms="8" android:scrollHorizontally="true" android:singleLine="true" android:text="" android:textColor="@color/white" android:textSize="@dimen/text_size_18" />
原文地址:http://blog.csdn.net/u012252502/article/details/30225929