在安卓系统中实现滚动文本视图

5
在我的应用程序中,我需要实现一个自动滚动的文本视图。我参考了此链接:这里
现在我有一个滚动文本视图。但是根据我的要求,我有一个字符串数组(我已经解析并有一些字符串)...考虑到数组可能是:
 string[] array=new string{"fgsdfd","gdfghdjhsd","gdfjhsgfhds"};

现在我希望这个数组能够显示在那个文本视图中(并且可以自动滚动)。
我想要的效果是这样的:
 fgsdfd   gdfghdjhsd    gdfjhsgfhds------------------>this will scroll automatically

这是我的滚动文本框:

 <TextView
    android:text="Really Long Scrolling Text Goes Here.... ..... ............ .... ...."
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:id="@+id/TextView03"
    android:padding="5dip" 
    android:layout_width="wrap_content" 
    android:textColor="#000000"
    android:layout_height="wrap_content" />

如何将字符串数组设置为TextView的文本…请帮忙。
3个回答

3
你可以使用 StringBuilder 将所有字符串合并为一个字符串,并将其应用到 TextView 中。
我已经实现了自己的文本视图,通过包装文本视图(和可能一些其他视图)来滚动。
private Runnable scrollRight = new Runnable()
{

    @Override
    public void run()
    {
                    // can control scrolling speed in on tick
        topScroll.smoothScrollBy(1, 0);
    }
};

在新线程中,我调用:

while (!interruptScroll){
    try{
        Thread.sleep(50); // control ticking speed
    }
    catch (InterruptedException e){
        e.printStackTrace();
    }
    topScroll.post(scrollRight);
}

我通过手动滚动scrollView来中断滚动(这样就不会被用户打断的自动滚动)。


顺便说一句 - 要实现自动滚动,请使用ScrollView、smoothScrollBy和线程/定时器。 - dilix
k...谢谢dilix...你能再帮我一个忙吗?在上面的代码中,滚动速度非常慢,我想让它们滚动得快一点。我该如何在上面的代码中实现呢? - Subburaj
将变量放在第一部分,然后在(new Thread("you code in Runnable")).start();中编写代码,你的滚动代码将在另一个线程中运行。 - dilix

1

尝试使用 StringBuilder。

String[] array = { "fgsdfd", "gdfghdjhsd", "gdfjhsgfhds" };
StringBuilder sb = new StringBuilder();

for (int i = 0; i < array.length; i++) {
    sb.append(array[i]);
}
txtView.setText(sb.toString());

0

试试这个:

<TextView
android:text="Really Long Scrolling Text Goes Here.... ..... ............ .... ...."
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollbars="horizontal"
android:id="@+id/TextView03"
android:padding="5dip" 
android:layout_width="wrap_content" 
android:textColor="#000000"
android:layout_height="wrap_content" />

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