Android自动水平滚动TextView

新手上路,请多包涵

我正在尝试实现一个会自动滚动的单行文本视图。但不幸的是,我无法让它发挥作用。 AutoScrollTextView 在 LinearLayout 中声明(宽度和高度 = fill_parent)。该类基本上使用一个处理程序,该处理程序调用自身以滚动给定数量。我已经简化了代码,只显示一个应该每秒滚动 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);
        }
    };
}

原文由 Vincent Mimoun-Prat 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 657
2 个回答

如果您不需要子类 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"/>

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

 findViewById(R.id.serviceColorCode).setSelected(true);

[根据评论编辑答案]

原文由 Rajath 发布,翻译遵循 CC BY-SA 3.0 许可协议

唯一对我有用的是使用滚动视图并以编程方式设置滚动

片段内的文本视图

<HorizontalScrollView
        android:id="@+id/hsv_fragment_drafting_now_header_info"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="8dp"
        android:scrollbars="none">

        <TextView
               android:id="@+id/tv_fragment_drafting_now_header_info"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:fontFamily="@font/circular_std_book"
               android:maxLines="1"
               android:text="A very long text that will be reaching out the screen or view container"
               android:textSize="16sp" />

</HorizontalScrollView>

动画,sliding_text.xml

 <translate xmlns:android="http://schemas.android.com/apk/res/android"
      android:duration="8000"
      android:fromXDelta="100%"
      android:interpolator="@android:anim/linear_interpolator"
      android:repeatCount="infinite"
      android:repeatMode="restart"
      android:toXDelta="-100%" />

阻止手动滚动并开始动画

headerInfoScroll.setOnTouchListener(object : View.OnTouchListener{
    override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
         return true
    }
})

headerInfo.startAnimation(AnimationUtils.loadAnimation(this.context, R.anim.sliding_text))

原文由 Felipe Silva Vieira 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题