Android TextView文字横向自动滚动(跑马灯)

TextView实现文字滚动需要以下几个要点:

  1.文字长度长于可显示范围:android:singleLine="true"
  2.设置可滚到,或显示样式:android:ellipsize="marquee"
  3.自定义滚动的ScrollingTextView
 public class ScrollingTextView extends TextView {

     public ScrollingTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} public ScrollingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
} public ScrollingTextView(Context context) {
super(context);
} @Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
if (focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
} @Override
public void onWindowFocusChanged(boolean focused) {
if (focused)
super.onWindowFocusChanged(focused);
} @Override
public boolean isFocused() {
return true;
} }

在布局里调用自定义的TextView:

 <com.example.rolltextdemo.ScrollingTextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/hello_world" />
上一篇:sql server基础学习之引号


下一篇:SAP从入门到精通 知识体系