当我添加更多文本行时,如何使TextView自动滚动?

Ear*_*rlz 33 user-interface android scroll textview

所以,我有一个像这样的TextView:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="140.7dp"
    android:id="@+id/terminalOutput"
    android:layout_marginBottom="0.0dp"
    android:scrollbars="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:maxLines="8" />
Run Code Online (Sandbox Code Playgroud)

我将它用作一种运行日志,显示给用户,以便他们可以监视大约需要3分钟的任务进度.但是,一旦我超过8行,文本就会消失.这对用户来说是不直观的,因为除了通过向下滚动手动轮询之外,他们无法知道它已经离开了屏幕.

我怎么能这样做,以便每次我添加一些文本到这个TextView我让它向下滚动尽可能低?

此外,这是在Xamarin Android,但我不认为这是相关的.在它和Java之间进行转换很容易

NOT*_*sak 70

从您的代码中,有两个步骤:

步骤1

虽然您在Xamarin Android中编码,但是在xxxActivity.java中的Java中,对于terminalOutput调用必须是这样的

TextView outputText = (TextView) findViewById(R.id.terminalOutput);
outputText.setMovementMethod(new ScrollingMovementMethod());
Run Code Online (Sandbox Code Playgroud)

方法setMovementMethod()通过参数ScrollingMovementMethod()是噱头.

第2步

在Java-Android的布局中,如上面的TextView声明的activity_xxx.xml中必须要添加这个

android:gravity="bottom"
Run Code Online (Sandbox Code Playgroud)

当你像这样在outputText中添加新行时

outputText.append("\n"+"New text line.");
Run Code Online (Sandbox Code Playgroud)

它会自动滚动到最后一行,这些都是你需要的魔力.

  • 设置重力+1会在没有它的情况下起作用 (9认同)

Dar*_*uis 33

根据这里的答案,在Android中制作TextView Scrollable

您实际上不需要使用ScrollView.

只需设置

android:maxLines = "AN_INTEGER"

android:scrollbars =布局的xml文件中TextView的"垂直"属性.

然后使用:

yourTextView.setMovementMethod(new ScrollingMovementMethod());

在你的代码中.

那可行..

  • 这个答案不正确,问题是指“自动滚动”。答案如下,使用重力。 (26认同)
  • 请注意 TextView 的高度非零。如果您按照 Android Studio 建议的性能将 `layout_height` 设置为 `0dp`,则自动滚动将不起作用 (2认同)
  • 不在棒棒糖伙伴工作.请更新你的答案. (2认同)

小智 7

这些答案都不是我想要的,所以我想出了这个.

textView.append(log);

while (textView.canScrollVertically(1)) {
    textView.scrollBy(0, 10);
}
Run Code Online (Sandbox Code Playgroud)

不要忘记在滚动之前设置移动方法

textView.setMovementMethod(new ScrollingMovementMethod());
Run Code Online (Sandbox Code Playgroud)

  • 如果您可以控制文本的附加位置,就像在您的示例中一样,您可以简单地在“textView.append(log)”后面加上“textView.bringPointIntoView(textView.length())”。 (2认同)

Ale*_*hko 6

有同样的问题.从这个和类似的讨论中尝试了几个决定,没有任何效果.解决这个问题:

edtConsoleText.setSelection(edtConsoleText.getText().length());
Run Code Online (Sandbox Code Playgroud)

每一次之后.append().


小智 5

您可以尝试2种解决方案:

  1. 将TextView放在ScrollView中

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
            <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Your Text" >
        </TextView>
    </ScrollView>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用自定义滚动TextView(与传统TextView相同,但它可以滚动)

    import android.content.Context;
    import android.graphics.Rect;
    import android.text.TextPaint;
    import android.util.AttributeSet;
    import android.view.animation.LinearInterpolator;
    import android.widget.Scroller;
    import android.widget.TextView;
    
     public class ScrollTextView extends TextView {
    
    
        // scrolling feature
        private Scroller mSlr;
    
        // milliseconds for a round of scrolling
        private int mRndDuration = 250;
    
        // the X offset when paused
        private int mXPaused = 0;
    
        // whether it's being paused
        private boolean mPaused = true;
    
        /*
         * constructor
         */
        public ScrollTextView(Context context) {
            this(context, null);
            // customize the TextView
            setSingleLine();
            setEllipsize(null);
            setVisibility(INVISIBLE);
        }
    
        /*
         * constructor
         */
        public ScrollTextView(Context context, AttributeSet attrs) {
            this(context, attrs, android.R.attr.textViewStyle);
            // customize the TextView
            setSingleLine();
            setEllipsize(null);
            setVisibility(INVISIBLE);
        }
    
        /*
         * constructor
         */
        public ScrollTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            // customize the TextView
            setSingleLine();
            setEllipsize(null);
            setVisibility(INVISIBLE);
        }
    
        /**
         * begin to scroll the text from the original position
         */
        public void startScroll() {
            // begin from the very right side
            mXPaused = -1 * getWidth();
            // assume it's paused
            mPaused = true;
            resumeScroll();
        }
    
        /**
         * resume the scroll from the pausing point
         */
        public void resumeScroll() {
    
            if (!mPaused)
                return;
    
            // Do not know why it would not scroll sometimes
            // if setHorizontallyScrolling is called in constructor.
            setHorizontallyScrolling(true);
    
            // use LinearInterpolator for steady scrolling
            mSlr = new Scroller(this.getContext(), new LinearInterpolator());
            setScroller(mSlr);
    
            int scrollingLen = calculateScrollingLen();
            int distance = scrollingLen - (getWidth() + mXPaused);
            int duration = (new Double(mRndDuration * distance * 1.00000
                    / scrollingLen)).intValue();
    
            setVisibility(VISIBLE);
            mSlr.startScroll(mXPaused, 0, distance, 0, duration);
            mPaused = false;
        }
    
        /**
         * calculate the scrolling length of the text in pixel
         * 
         * @return the scrolling length in pixels
         */
        private int calculateScrollingLen() {
            TextPaint tp = getPaint();
            Rect rect = new Rect();
            String strTxt = getText().toString();
            tp.getTextBounds(strTxt, 0, strTxt.length(), rect);
            int scrollingLen = rect.width() + getWidth();
            rect = null;
            return scrollingLen;
        }
    
        /**
         * pause scrolling the text
         */
        public void pauseScroll() {
            if (null == mSlr)
                return;
    
            if (mPaused)
                return;
    
            mPaused = true;
    
            // abortAnimation sets the current X to be the final X,
            // and sets isFinished to be true
            // so current position shall be saved
            mXPaused = mSlr.getCurrX();
    
            mSlr.abortAnimation();
        }
    
        @Override
        /*
         * override the computeScroll to restart scrolling when finished so as that
         * the text is scrolled forever
         */
        public void computeScroll() {
            super.computeScroll();
    
            if (null == mSlr)
                return;
    
            if (mSlr.isFinished() && (!mPaused)) {
                this.startScroll();
            }
        }
    
        public int getRndDuration() {
            return mRndDuration;
        }
    
        public void setRndDuration(int duration) {
            this.mRndDuration = duration;
        }
    
        public boolean isPaused() {
            return mPaused;
        }
     }
    
    Run Code Online (Sandbox Code Playgroud)

如何使用:

ScrollTextView scrolltext = (ScrollTextView) findViewById(R.id.YourTextView);
Run Code Online (Sandbox Code Playgroud)

(ScrollTextView类源:http://bear-polka.blogspot.com/2009/01/scrolltextview-scrolling-textview-for.html)