自动滚动文本垂直

时间:2016-06-07 05:36:59

标签: android scroll textview vertical-scrolling

如何以垂直方式自动滚动直到文本结束?此代码在一段时间后停止并且未到达结尾。而且我想在点击按钮后开始滚动直到结束。一些帮助就足够了。

activity_main.xml中

<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" >
<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textview1"
    android:layout_alignParentRight="true"
    android:text="OK" />
<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" >

    <TextView
        android:text="@string/random_text"
        android:id="@+id/textview1"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:scrollbars="vertical"
        android:focusableInTouchMode="true"
        android:maxLines="4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
 </ScrollView>
</RelativeLayout>

MainActivity.java

package com.bhabani2077.autoscroll;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.Scroller;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
Toolbar mToolbar;
// EditText mEditText;
Button mButton;
TextView mTextView;
Scroller mScroller;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mToolbar= (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
  //  mEditText= (EditText) findViewById(R.id.editText1);
    mButton= (Button) findViewById(R.id.button1);
    mTextView= (TextView) findViewById(R.id.textview1);
    mTextView.setSelected(true);
    mTextView.setMovementMethod(new ScrollingMovementMethod());
    mScroller=new Scroller(MainActivity.this,new LinearInterpolator());
    mTextView.setScroller(mScroller);
    mScroller.startScroll(0,0,0,500,5000);

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //if (mEditText.getText().length()>0){
            //    mTextView.append(mEditText.getText()+"\n");
             //   mEditText.setText("");

            }
    });

}
 }

2 个答案:

答案 0 :(得分:2)

我想我自己找到了解决办法。我使用Scroller方法创建了一个自定义TextView类,并将其链接到XML代码。看看,

<强> AutoScrollingText.java

public class AutoScrollingText extends TextView {

    private static final float DEFAULT_SPEED = 9.0f;

    public Scroller scroller;
    public float speed = DEFAULT_SPEED;
    public boolean continuousScrolling = true;

    public AutoScrollingText(Context context) {
        super(context);
      scrollerInstance(context);
    }

    public AutoScrollingText(Context context, AttributeSet attributes) {
        super(context, attributes);
       scrollerInstance(context);
    }

    public void scrollerInstance(Context context) {
        scroller = new Scroller(context, new LinearInterpolator());
        setScroller(scroller);
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (scroller.isFinished()) {
           scroll();
        }
    }

    public void scroll() {
        int viewHeight = getHeight();
        int visibleHeight = viewHeight - getPaddingBottom() - getPaddingTop();
        int lineHeight = getLineHeight();

        int offset = -1 * visibleHeight;
        int distance = visibleHeight + getLineCount() * lineHeight;
        int duration = (int) (distance * speed);


        scroller.startScroll(0, offset, 0, distance, duration);
    }



    public void setSpeed(float speed) {
        this.speed = speed;
    }

    public float getSpeed() {
        return speed;
    }

    public void setContinuousScrolling(boolean continuousScrolling) {
        this.continuousScrolling = continuousScrolling;
    }

    public boolean isContinuousScrolling() {
        return continuousScrolling;
    }

}

<强> MainActivity.java

public class MainActivity extends AppCompatActivity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    }


}

<强> activity_main.xml中

  <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"
            android:elevation="10dp"
            tools:context=".MainActivity" >


    <com.bhabani2077.scrolltext.AutoScrollingText
        android:text="@string/random_text"
        android:id="@+id/textview1"
        android:focusable="true"
        android:scrollHorizontally="false"
        android:scrollbars="vertical"
        android:ellipsize="marquee"
        android:layout_width="match_parent"
        android:marqueeRepeatLimit ="marquee_forever"
        android:layout_height="200dp"/>

 </RelativeLayout>

答案 1 :(得分:0)

您不需要ScrollView

在textview中设置属性

android:maxLines = "add your integer value"
android:scrollbars = "vertical"

点击

mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           mTextView.setMovementMethod(new ScrollingMovementMethod());
            }
    });