【问题标题】:Cannot auto scroll text in TextView无法在 TextView 中自动滚动文本
【发布时间】:2014-03-09 19:49:07
【问题描述】:

当我的文本太长时,我试图让 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 都不会滚动。有什么问题?

EDIT[NEW]:我注意到它只在第一次工作。其他时候我更改了 TextView 上的文本,它不再起作用了。它能做什么?我有每 50 毫秒唤醒一次以更新 SeekBar 和其他两个 TextView 的线程(定时器),这可能会阻塞某些东西?

【问题讨论】:

  • 您希望它水平滚动还是垂直滚动?
  • 横向滚动比较好,如果只能纵向滚动就好了

标签: android scroll textview autoscroll


【解决方案1】:

TextView 在处理滚动时有时会很棘手。您必须在布局中设置的主要属性如下:

android:scrollHorizontally="false"
android:scrollbars="vertical"
android:gravity="bottom"

每次追加一行时,将gravity 设置为底部将使滚动条下降到TextView 的底部。所以只有当你希望它是这样时才使用它。

music_name.setMovementMethod(new ScrollingMovementMethod()); 语句也是必需的。

这样它应该可以工作。

【讨论】:

  • 我在我的 XML 上更改了它,但它不起作用。它只是保留了一个静态文本,缺少部分文本。
  • 那个TextViewsingleLine是故意的吗?如果它只有一行,为什么需要滚动?
  • 之所以存在是因为最初的想法是创建一个只有一行的水平滚动。但是从 XML 中删除它也不能解决问题。它只是因为重力=“底部”而使文本改变方向,但文本不会自动滚动。
  • 澄清一下:'autoscroll' 是指每次调用 append 时,textview 会自动滚动到内容的底部?您是否添加了足够的文本以使滚动启动?您定义它的方式,它只会在需要时触发。
  • 我在想一些东西,TextView 上的文本一直在无限循环滚动以显示整个文本。我不知道这是否是 TextView 所做的,但我试图找到这种效果。
【解决方案2】:

你需要使用一些动画,比如 res 的 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));
        }
    });

【讨论】:

    【解决方案3】:
    <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"/>
    

    【讨论】:

    • 全文不可见,我的意思是它超过了“...”
    【解决方案4】:
    <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
        }
    
    }) }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      相关资源
      最近更新 更多