TextView无法自动滚动文本

3

我想要一个自动滚动的TextView,当文本过长时。我按照这里在stackoverflow发布的每个教程或提示(是的,我阅读了许多类似于这个问题的主题,但没有人解决它)。

所以我的活动布局就像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

....

<TextView
    android:id="@+id/music_name"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:gravity="center"
    android:text="Music Name"
    android:textSize="24sp"
    android:layout_centerHorizontal="true"
    android:singleLine="true"
    android:scrollHorizontally="true"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever" />

....

</RelativeLayout>

而在OnCreate( )中,我这样做:

music_name.setSelected(true);
music_name.setMovementMethod(new ScrollingMovementMethod());

music_name是一个TextView。

即使我做了所有的事情,我的TextView也不会滚动。可能出了什么问题?

编辑[新]: 我注意到它只在第一次运行时有效。其他时候当我改变TextView上的文本时,它就不再起作用了。这可能是什么原因呢?我有一个线程(Timer),每50毫秒唤醒一次以更新SeekBar和其他两个TextView,这可能阻塞了某些东西吗?


你想让它水平滚动还是垂直滚动? - nKn
最好可以水平滚动,但如果只能垂直滚动也可以。 - ferrazrafael
4个回答

2

TextView 在处理滚动时有时会变得棘手。您需要在布局中设置以下主要属性:

android:scrollHorizontally="false"
android:scrollbars="vertical"
android:gravity="bottom"
gravity 设置为 bottom 可以使得每次添加一行时,滚动条自动滑到 TextView 底部。如果您希望这样,请使用此选项。
同样需要加上 music_name.setMovementMethod(new ScrollingMovementMethod()); 语句。
这样就应该可以工作了。

我修改了XML文件,但它无法工作。它只是保留了静态文本,缺失了部分内容。 - ferrazrafael
这个 TextView 的 singleLine 属性是有意设置的吗?如果它只有一行,为什么还需要滚动呢? - nKn
它存在的原因是最初的想法是创建一个只有一行的水平滚动。但是从XML中删除它并不能解决问题。这只会使文本因为gravity="bottom"而改变方向,但文本不会自动滚动。 - ferrazrafael
只是澄清一下:您所说的“自动滚动”,是指每次调用其附加功能时,文本视图会自动滚动到内容底部吗? 您是否添加了足够的文本以使滚动条启动?按您定义的方式,它只会在必要时才触发。 - nKn
我在考虑一个文本视图可以无限循环滚动以显示整个文本的效果。我不知道TextView是否支持这种效果,但我正在尝试寻找这种效果。 - ferrazrafael
然后我会切换回singleLine选项,禁用垂直滚动,启用水平滚动(如果您想要的话甚至可以禁用),您要寻找的效果是TextView的一个属性称为ellipsize。我认为你想做的是marquee效果。只需设置android:ellipsize="marquee",看看它是否符合您的要求。 - nKn

0
<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="0dp">

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</ScrollView>

并定义addTextChangedListener

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

        //some code here

    ScrollView scrollView1 = (ScrollView) findViewById(R.id.scrollView1);
    TextView textView1 = (TextView) findViewById(R.id.textView1);
    textView1.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable arg0) {
        scrollView1.fullScroll(ScrollView.FOCUS_DOWN);
        // you can add a toast or whatever you want here
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,     int arg3) {
        //override stub
    }

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int     arg3) {
        //override stub
    }

}) }

0
<TextView
    android:id="@+id/mytext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="very long text to srollll abcdefghijklmnopqrstdaghlfj'ogjpsroshsrfjhrdjdjkdjdkjdk" 
    android:singleLine="true" 
    android:marqueeRepeatLimit="marquee_forever" 
    android:ellipsize="marquee"
    android:focusable="true" 
    android:focusableInTouchMode="true"/>

完整的文本不可见,因为它超过了“...” - Vaishali Sutariya

0
你需要使用一些动画,比如在资源文件夹的anim文件夹中使用scroll_textview_animation.xml这样的动画:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fromXDelta="200"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toXDelta="-200" />

在onCreate方法中或者设置文本之后:

music_name.startAnimation((Animation)AnimationUtils.loadAnimation(Context,R.anim.scroll_textview_animation));

或者您可以在点击时开始动画:

music_name.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            music_name.startAnimation((Animation)AnimationUtils.loadAnimation(Context,R.anim.scroll_textview_animation));
        }
    });

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