【问题标题】:Android automatic horizontally scrolling TextViewAndroid自动水平滚动TextView
【发布时间】:2011-07-25 05:57:34
【问题描述】:

我正在尝试实现一个自动滚动的单行文本视图。但不幸的是,我无法让它发挥作用。 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);
        }
    };
}

【问题讨论】:

  • 通常对于滚动文本,它应该是焦点。您是否尝试过专注于您的布局?还是对视图说 setSelected()?
  • 是的,尝试从 Activity 和自定义小部件中的 Handler 执行此操作。

标签: android scroll textview


【解决方案1】:

如果你不需要子类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);

[根据cmets编辑的答案]

【讨论】:

  • 有没有办法让这条选框线在完成一个循环后返回(反向)?
  • 是否可以提高滚动速度?
  • TextView 必须设置为选中:textView.setSelected(true);
  • 请注意它应该是maxLines="1",因为singleLine已被弃用
  • 您必须使用 singleLine 属性才能使其工作。仅在 AppCompatTextView 上尝试使用 maxLines,什么也没有。所以 singleLine 和 setSelected(..) 是关键。
【解决方案2】:

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

<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"/>

我们需要设置

TextView tv=(TextView)findViewById(R.id.textview1);
  tv.setSelected(true); 

这终于让我的工作了

【讨论】:

  • 滚动浏览时应该可以看到整个文本。结尾的文字是点的(太......)。所以没有滚动的意义。请提出解决方案。
  • 我想你可能已经设置了 android:ellipsize="end"。检查一次。如果不是这样,请告诉我。另外,如果可能,请在此处分享您的代码
  • 不,我有“字幕”。但我在回收站视图中这样做。外部 recyclerview 有一个包含 textview 的元素。然后文本视图有很长的文本。我想在一行中显示它。这将自动滚动无限。
  • 好的。不知道为什么使用它是回收站视图会禁用自动滚动。
【解决方案3】:

我的解决方案有效:

<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"/>

并且TextView 必须被设置为选中: textView.setSelected(true);onCreate 中的代码为例。

【讨论】:

    【解决方案4】:
    // this TextView will marquee because it is selected
    TextView marqueeText1 = (TextView) findViewById(R.id.marquee_text_1);
    marqueeText1.setSelected(true);
    
    <TextView
        android:id="@+id/marquee_text_1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit."
        android:textSize="24sp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true" />
    

    【讨论】:

      【解决方案5】:

      为了移动文本,除了添加属性:

      android:ellipsize="marquee"
      android:singleLine="true"
      android:marqueeRepeatLimit="marquee_forever"
      android:focusable="true"
      android:clickable="true"
      android:focusableInTouchMode="true"
      android:scrollHorizontally="true"
      

      View 必须处于焦点位置,因此可以通过两种方式完成:

      1. 以编程方式:

        tv.setSelected (true);
        
      2. 通过添加 requestFocus 标签在 XML 中执行所有操作:

        <TextView 
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:ellipsize="marquee"
             android:singleLine="true"
             android:marqueeRepeatLimit="marquee_forever"
             android:focusable="true"
             android:clickable="true"
             android:focusableInTouchMode="true"
             android:scrollHorizontally="true">
             <requestFocus />
        </TextView>
        

      【讨论】:

      • 谢谢。我想知道为什么其他答案中的所有解决方案都对我不起作用。因为没有指令android:clickable="true"
      【解决方案6】:

      提示:您的文本长度应该很大,否则如果它不超过您的屏幕尺寸,它将不适用于长度较小的文本。

      <  TextView
              android:id="@+id/support"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text=" enter the large text that wont fit on screen "
              android:singleLine="true"
              android:scrollHorizontally="true"
              android:ellipsize="marquee"
              android:marqueeRepeatLimit="marquee_forever"
              />
      

      在你的活动中

       TextView textView=findViewById(R.id.support);
              textView.setSelected(true);
      

      【讨论】:

        【解决方案7】:

        在主线程中调用 invalidate 之前,有时 invalidate 不起作用,如下所示:

        handler.post(new Runnable() {
        
                @Override
                public void run() {
                    yourView.invalidate();
                }
            });
        

        【讨论】:

          【解决方案8】:

          setMovementMethod(new ScrollingMovementMethod())setHorizo​​ntallyScrolling(true) 在我的情况下有效。

          在.java中:

          tv = findViewById(R.id.textViewID);
          tv.setMovementMethod(new ScrollingMovementMethod());
          tv.setHorizontallyScrolling(true);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-11-11
            • 1970-01-01
            • 1970-01-01
            • 2011-03-20
            • 2020-08-17
            • 1970-01-01
            • 2011-06-28
            • 1970-01-01
            相关资源
            最近更新 更多