安卓:如何在TextView中实现文本自动滚动

5

我已经创建了一系列固定宽度为100px的选项卡。一个选项卡包含一个带有下面文本的图像。如果文本太长不能适应,我希望它能自动滚动,但我只希望有一行文本。我试过以下方法,但是没有成功。我正在使用Android 2.3:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginRight="3dp"
    android:layout_marginTop="3dp"
    android:background="#ff737373"
    android:gravity="center"
    android:minWidth="64dp"
    android:orientation="vertical"
    android:padding="3dp" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="tabImage" >
    </ImageView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:maxWidth="100px"
        android:tag="tabCaption"
        android:textColor="#ffd9d9d9"
        android:textSize="16sp" />

</LinearLayout>

您知道为什么这不起作用吗?我在另一个帖子中找到了解决方案,那里的用户表示它有效。

4个回答

11
在你的活动中,如果你想要滚动文本,你需要添加if语句。
  TextView tv=(TextView)findViewById(R.id.textview1);
  tv.setSelected(true);

发布了这个之后,我又进行了一些搜索,并发现了这个。无论如何,非常感谢! - Johann


0
<TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:gravity="center"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:text="Auto text scroller" 
        android:textSize="50sp"
        />

0
请注意,如果文本的长度小于其定义的边界宽度,则不会滚动。在这种情况下,您可能希望使用自身扩展文本,例如:

-. . . . .

String charsInBreak = "   ";
 while (bounds.width() < this.m_width)
 {
    m_text = (m_text + charsInBreak + m_text);
    paint.getTextBounds(m_text, 0, m_text.length(), bounds);
 } 

如果你想让你的文本永远滚动:

m_textView.setMarqueeRepeatLimit(-1);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接