簡體   English   中英

如何在添加更多行文本時使 TextView 自動滾動?

[英]How can I make a TextView automatically scroll as I add more lines of text?

所以,我有一個像這樣的 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" />

我將它用作一種運行日志,向用戶顯示,以便他們可以監控大約需要 3 分鍾的任務的進度。 但是,一旦我超過 8 行,文本就會離開屏幕。 這對用戶來說是不直觀的,因為他們無法知道它離開了屏幕,只能通過嘗試向下滾動來手動輪詢。

我怎樣才能做到這樣,每次我向這個 TextView 添加一些文本時,我都會讓它向下滾動到盡可能低的位置?

此外,這是在 Xamarin Android 中,但我認為它不相關。 它和 Java 之間的轉換很容易

從您的代碼中,必須執行兩個步驟:

第1步

雖然你在Xamarin Android 中編碼,但是就像在Java 中的xxxActivity.java for terminalOutput invoke 必須是這樣的

TextView outputText = (TextView) findViewById(R.id.terminalOutput);
outputText.setMovementMethod(new ScrollingMovementMethod());

方法 setMovementMethod() 通過參數 ScrollingMovementMethod() 是噱頭。

第2步

並且在 Java-Android 中的布局中,也在activity_xxx.xml 中為上面的 TextView 聲明必須添加這個

android:gravity="bottom"

當您像這樣在 outputText 中添加新行時:

outputText.append("\n"+"New text line.");

它會自動滾動到最后一行,這些都是您需要的魔法。

根據這里的答案使 TextView 在 Android 中可滾動

您實際上不需要使用 ScrollView。

只需設置

android:maxLines = "AN_INTEGER"

android:scrollbars = "vertical" 布局的 xml 文件中 TextView 的屬性。

然后使用:

yourTextView.setMovementMethod(new ScrollingMovementMethod());

在你的代碼中。

那可行..

有同樣的問題。 從這個和類似的討論中嘗試了幾個決定,沒有任何效果。 是這樣解決的:

edtConsoleText.setSelection(edtConsoleText.getText().length());

在每個.append()

這些答案都不是我想要的,所以我想出了這個。

textView.append(log);

while (textView.canScrollVertically(1)) {
    textView.scrollBy(0, 10);
}

滾動前不要忘記設置移動方法

textView.setMovementMethod(new ScrollingMovementMethod());

您可以嘗試兩種解決方案:

  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>
  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; } }

如何使用:

ScrollTextView scrolltext = (ScrollTextView) findViewById(R.id.YourTextView);

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

從“如何在活動啟動時在 ScrollView 中滾動到底部”:

 final ScrollView scrollview = ((ScrollView) findViewById(R.id.scrollview)); scrollview.post(new Runnable() { @Override public void run() { scrollview.fullScroll(ScrollView.FOCUS_DOWN); } });

您需要的是在追加操作之后放置fullScroll()

我正在使用 Xmarin。

我的解決方案和很多人提到的一樣,是在 ScrollView 中的 textView。

如果要在視圖底部看到新行,請使用

android:layout_gravity="bottom"

這會將新行保留在底部,直到您滾動視圖。 視圖停留在您滾動的任何位置。

不需要代碼。

但是,如果希望內容在追加后始終在底部,則需要在 append() 后添加代碼:

myText.Append(...);

myscroll.FullScroll(FocusSearchDirection.Down);

對我來說,除了這兩者的組合外,沒有什么是完美的:-

  • 設置scroller.scrollTo(0, 70); 在您的 java 類中。在設置 textview 之前但在附加該字符串之后使用它。 52 dp 是我設備的高度。你可以使用 scroller.getBottom()) 找到它; 所以我用 70 來調整滾動視圖。
  • 在你的android:scrollY="30dp"設置android:scrollY="30dp"

就我而言,一開始沒有任何效果,因為我顯然是在嘗試在 UI 線程之外對 UI 進行更改(添加不在視圖中的文本后自動向上滾動)。 單詞會顯示,但沒有自動滾動。 更正, setText 可以工作但不能追加。

我對自動滾動的調用不在 UI 線程中,因為我編寫了響應套接字 onMessage 調用的調用; 我在它自己的線程上運行。 因此,我不得不將我的函數調用包含在以下代碼中,並且一切正常。

 runOnUiThread(new Runnable() {
                 @Override
                 public void run() {

                     // Stuff that updates the UI

                 }
             });

我敢肯定,只要您在 UI 線程上運行它,所有方法都會以某種方式工作。 但是,為了實現“聊天室”的自動滾動效果,即聊天窗口頂部的新文本但是當文本量比窗口長時 - 將新消息自動滾動到視圖中,我只是使用了 ... fullScroll(View .FOCUS_DOWN) ... 方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM