真正的TextView跑马灯
jopen
10年前
android自带的跑马灯是必须在有焦点的情况下才会滚动,一旦失去焦点跑马灯就会失去效果。
现在我的做法是自定义TextView 并重写isFocused和onFocusChanged方法,设置focused为true,这样TextView就可以始终获取到焦点
亲测可用!
1。自定义TextView:
</div> </div> </div>
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中引用:
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);//重点 } }来自:http://blog.csdn.net/u012252502/article/details/30225929