簡體   English   中英

如何在textView中有很多文本,但只顯示其中一些

[英]How to have a lot of text in textView but only display some of it

我確信網絡上有很多關於我遇到的問題的資料,但是由於我是Android應用程序開發的新手,所以我真的不知道如何提出問題(不知道這些術語)。

我的應用程序中有兩個按鈕和一個textView:

<TextView
    android:layout_width="50dp"
    android:layout_height="100dp"
    android:text="P1"
    android:id="@+id/history"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal" />

當按下按鈕時,一個字符串將附加到textView(在新行中):

switch (v.getId()) {

        case R.id.button1:
            counter -= 20;                
            stack.push(20);
            history.append("\r\n " + seznam.peek());
            break;

        case R.id.button2:
            counter -= 1;                
            stack.push(1);
            history.append("\r\n " + seznam.peek());
            break;

        default: break;
    }

目前,當沒有足夠的空間容納新行(100dp)時,只有舊行可見,而新行則不可見。

我希望最新的行始終可見,並且我可以選擇查看舊行(通過滑動)..(類似無滾動條的iframe)

您實際上不需要使用ScrollView

只需設置

android:maxLines = "AN_INTEGER"

android:scrollbars = "vertical"

布局的xml文件中TextView的屬性。

然后使用:

yourTextView.setMovementMethod(new ScrollingMovementMethod());

在您的代碼中。

它會自動滾動,沒有任何問題。

暫無
暫無

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

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