Android自动水平滚动TextView

Vin*_*rat 58 android scroll textview

我正在尝试实现一个自动滚动的单行文本视图.但我不幸的是无法让它发挥作用.AutoScrollTextView在LinearLayout(width和height = fill_parent)中声明.该类基本上使用一个Handler,它调用自己按给定的量滚动.我已将代码简化为仅显示应每秒滚动5个像素的文本视图.

日志输出正确,getScrollX()方法返回相应的scrollX位置.

如果我不打电话requestLayout(),就不会有任何吸引力.invalidate()没有效果.

有人会有线索吗?

public class AutoScrollTextView extends TextView {

    public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setSingleLine();
        setEllipsize(null);
        setText("Single-line text view that scrolls automatically if the text is too long to fit in the widget");
    }

    // begin to scroll the text from the original position
    public void startScrolling() {
        scrollHandler.sendEmptyMessage(0);
    }

    private Handler scrollHandler = new Handler() {
        private static final int REFRESH_INTERVAL = 1000;

        public void handleMessage(Message msg) {
            scrollBy(5, 0);
            requestLayout();
            Log.debug("Scrolled to " + getScrollX() + " px");
            sendEmptyMessageDelayed(0, REFRESH_INTERVAL);
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

raj*_*ath 191

如果您不需要对其进行子类化TextView,可以在布局文件中尝试:

    <TextView
        android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget" 
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true" 
        android:scrollHorizontally="true"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

此外,在您的代码中使用以下内容:

findViewById(R.id.serviceColorCode).setSelected(true);
Run Code Online (Sandbox Code Playgroud)

[根据评论编辑的答案]

  • 必须设置TextView:textView.setSelected(true); (25认同)
  • 是否有任何可能的方法使这个选框线在完成一个循环时返回(反向)? (21认同)
  • 请注意它应该是`maxLines ="1"`因为`singleLine`已被弃用 (6认同)
  • 是否可以提高滚动速度? (3认同)
  • 您必须使用 singleLine 属性才能使其工作。仅尝试使用 maxLines,在 AppCompatTextView 上,什么也没有。所以 singleLine 和 setSelected(..) 是关键。 (2认同)
  • 我如何做同样的事情,但垂直滚动? (2认同)

Suj*_*hta 18

在这些xml代码之后由@rajat回答

<TextView
        android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget" 
        android:singleLine="true" 
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true" 
        android:scrollHorizontally="true"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

我们需要设定

TextView tv=(TextView)findViewById(R.id.textview1);
  tv.setSelected(true); 
Run Code Online (Sandbox Code Playgroud)

这最终使我的工作


alf*_*ibg 16

我的解决方案有效

<TextView
     android:id="@+id/titolotxt"
     android:layout_width="..."
     android:layout_height="..."
     android:ellipsize="marquee"
     android:gravity="left"
     android:marqueeRepeatLimit="marquee_forever"
     android:singleLine="true"
     android:text="@string/titolo"/>
Run Code Online (Sandbox Code Playgroud)

并且TextView必须设置选择: 例如textView.setSelected(true);通过代码onCreate.