867

我在 TextView 中显示的文本似乎太长而无法放入一个屏幕。我需要让我的 TextView 可滚动。我怎样才能做到这一点?

这是代码:

final TextView tv = new TextView(this);
tv.setBackgroundResource(R.drawable.splash);
tv.setTypeface(face);
tv.setTextSize(18);
tv.setTextColor(R.color.BROWN);
tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
tv.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent e) {
        Random r = new Random();
        int i = r.nextInt(101);
        if (e.getAction() == e.ACTION_DOWN) {
            tv.setText(tips[i]);
            tv.setBackgroundResource(R.drawable.inner);
        }
        return true;
    }
});
setContentView(tv);
4

32 回答 32

1918

ScrollView您实际上不需要使用 a 。

只需设置

android:scrollbars = "vertical"

TextView您在布局的 xml 文件中的属性。

然后使用:

yourTextView.setMovementMethod(new ScrollingMovementMethod());

在你的代码中。

宾果游戏,它滚动!

于 2010-07-15T14:07:04.643 回答
328

这就是我纯粹在 XML 中所做的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ScrollView
        android:id="@+id/SCROLLER_ID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:fillViewport="true">

        <TextView
            android:id="@+id/TEXT_STATUS_ID"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"/>
    </ScrollView>
</LinearLayout>

笔记:

  1. android:fillViewport="true"结合android:layout_weight="1.0"将使 textview 占用所有可用空间。

  2. 定义滚动视图时,请勿指定,android:layout_height="fill_parent"否则滚动视图不起作用!(这让我刚才浪费了一个小时!FFS)。

专家提示:

要在附加文本后以编程方式滚动到底部,请使用以下命令:

mTextStatus = (TextView) findViewById(R.id.TEXT_STATUS_ID);
mScrollView = (ScrollView) findViewById(R.id.SCROLLER_ID);

private void scrollToBottom()
{
    mScrollView.post(new Runnable()
    {
        public void run()
        {
            mScrollView.smoothScrollTo(0, mTextStatus.getBottom());
        }
    });
}
于 2011-12-16T09:16:00.270 回答
120

真正需要的只是 setMovementMethod()。这是一个使用 LinearLayout 的示例。

文件main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:id="@+id/tv1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/hello"
    />
</LinearLayout>

文件WordExtractTest.java

public class WordExtractTest extends Activity {

    TextView tv1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv1 = (TextView)findViewById(R.id.tv1);

        loadDoc();
    }

    private void loadDoc() {

        String s = "";

        for(int x=0; x<=100; x++) {
            s += "Line: " + String.valueOf(x) + "\n";
        }

        tv1.setMovementMethod(new ScrollingMovementMethod());

        tv1.setText(s);
    }
}
于 2011-04-12T17:23:15.990 回答
51

让你的文本视图只是添加这个

TextView textview= (TextView) findViewById(R.id.your_textview_id);
textview.setMovementMethod(new ScrollingMovementMethod());
于 2013-07-26T01:59:59.997 回答
46

没有必要放入

android:Maxlines="AN_INTEGER"`

您只需添加以下内容即可完成您的工作:

android:scrollbars = "vertical"

并且,将此代码放入您的 Java 类中:

textview.setMovementMethod(new ScrollingMovementMethod());
于 2016-06-05T04:38:38.343 回答
29

你可以

  1. TextView由 a包围ScrollView;或者
  2. 将运动方法设置为ScrollingMovementMethod.getInstance();.
于 2010-01-19T10:04:51.467 回答
27

我发现的最好方法:

将 TextView 替换为具有以下额外属性的 EditText:

android:background="@null"
android:editable="false"
android:cursorVisible="false"

无需包装在 ScrollView 中。

于 2012-02-20T15:35:40.693 回答
18

简单的。我是这样做的:

  1. XML 方面:

    <?xml version="1.0" encoding="utf-8"?>
    <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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        tools:context="com.mbh.usbcom.MainActivity">
        <TextView
            android:id="@+id/tv_log"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:text="Log:" />
    </RelativeLayout>
    
  2. Java端:

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

奖金:

要让文本视图在文本填充时向下滚动,您必须添加:

    android:gravity="bottom"

到 TextView xml 文件。随着更多文本进入,它将自动向下滚动。

当然,您需要使用 append 函数而不是 set text 添加文本:

    tv_log.append("\n" + text);

我将它用于日志目的。

我希望这有帮助 ;)

于 2016-02-05T08:23:42.213 回答
15

这是“如何将 ScrollBar 应用到您的 TextView”,仅使用 XML。

首先,您需要在main.xml文件中获取一个 Textview 控件并在其中写入一些文本......像这样:

<TextView
    android:id="@+id/TEXT"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="@string/long_text"/>

接下来,将文本视图控件放置在滚动视图之间以显示此文本的滚动条:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_height="150px"
        android:layout_width="fill_parent">

        <TextView
            android:id="@+id/TEXT"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/long_text"/>

    </ScrollView>
</RelativeLayout>

而已...

于 2012-06-06T07:49:03.317 回答
11

上面来自 Somewhere 的“专业提示”(使 TextView 在 Android 上可滚动)效果很好,但是,如果您将文本动态添加到 ScrollView 并且希望在用户处于附加状态时自动滚动到底部,该怎么办? ScrollView 的底部?(也许是因为如果用户向上滚动阅读了一些您不想在追加期间自动重置到底部的内容,这会很烦人。)

无论如何,这里是:

if ((mTextStatus.getMeasuredHeight() - mScrollView.getScrollY()) <=
        (mScrollView.getHeight() + mTextStatus.getLineHeight())) {
    scrollToBottom();
}

mTextStatus.getLineHeight() 将使您不会在用户位于 ScrollView 末尾的一行内时使用 scrollToBottom()。

于 2013-05-20T16:46:26.090 回答
11

这将提供带有滚动条的平滑滚动文本。

ScrollView scroller = new ScrollView(this);
TextView tv = new TextView(this);
tv.setText(R.string.my_text);
scroller.addView(tv);
于 2012-12-20T12:29:03.003 回答
10

如果您希望在 textview 中滚动文本,则可以按照以下步骤操作:

首先,您应该对 textview 进行子类化。

然后使用它。

以下是子类文本视图的示例。

public class AutoScrollableTextView extends TextView {

    public AutoScrollableTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setEllipsize(TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
        setSingleLine();
        setHorizontallyScrolling(true);
    }

    public AutoScrollableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setEllipsize(TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
        setSingleLine();
        setHorizontallyScrolling(true);
    }

    public AutoScrollableTextView(Context context) {
        super(context);
        setEllipsize(TruncateAt.MARQUEE);
        setMarqueeRepeatLimit(-1);
        setSingleLine();
        setHorizontallyScrolling(true);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if(focused)
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean focused) {
        if(focused)
            super.onWindowFocusChanged(focused);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

现在,您必须以这种方式在 XML 中使用它:

 <com.yourpackagename.AutoScrollableTextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="This is very very long text to be scrolled"
 />

而已。

于 2012-12-27T10:06:07.463 回答
9

在 XML 的 textview 中添加以下内容。

android:scrollbars="vertical"

最后,添加

textView.setMovementMethod(new ScrollingMovementMethod());

在 Java 文件中。

于 2014-09-05T17:21:35.053 回答
9

在 kotlin 中用于使 textview 可滚动

myTextView.movementMethod= ScrollingMovementMethod()

并在 xml 中添加此属性

    android:scrollbars = "vertical"
于 2019-05-29T08:01:18.793 回答
6

我没有发现 TextView 滚动以支持“轻弹”手势,它在向上或向下轻弹后继续滚动。我最终自己实现了它,因为出于各种原因我不想使用 ScrollView,而且似乎没有一个 MovementMethod 允许我选择文本并单击链接。

于 2011-05-15T15:59:36.673 回答
5

完成可滚动后,在视图中输入任何内容时,将此行添加到视图的最后一行:

((ScrollView) findViewById(R.id.TableScroller)).fullScroll(View.FOCUS_DOWN);
于 2012-07-04T13:35:56.090 回答
5

如果你使用Kotlin,这样: XML

 <TextView
            android:id="@+id/tvMore"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="3"
            android:scrollbars="vertical" />

活动

tvMore.movementMethod = ScrollingMovementMethod()
于 2021-04-15T08:46:20.537 回答
4

将此添加到您的 XML 布局中:

android:ellipsize="marquee"
android:focusable="false"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="To Make An textView Scrollable Inside The TextView Using Marquee"

在代码中,您必须编写以下行:

textview.setSelected(true);
textView.setMovementMethod(new ScrollingMovementMethod());
于 2014-01-06T09:24:17.910 回答
4

如果您不想使用 EditText 解决方案,那么您可能会有更好的运气:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourLayout);
    (TextView)findViewById(R.id.yourTextViewId).setMovementMethod(ArrowKeyMovementMethod.getInstance());
}
于 2012-10-27T14:38:29.280 回答
4
yourtextView.setMovementMethod(new ScrollingMovementMethod());

你现在可以滚动它。

于 2020-01-07T06:03:25.890 回答
3

我知道这是一篇较旧的帖子,但这就是我在 Java 方面处理问题的方式。

    // Allow textView to scroll
    tv.setSingleLine(true);
    tv.setHorizontallyScrolling(true);
    tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);
    tv.setMarqueeRepeatLimit(-1); // Infinite
    // TextView must be 'selected'
    tv.setSelected(true);
    // Padding not necessary, but this helps so the text isn't right
    // up against the side of a screen/layout
    tv.setPadding(10, 0, 10, 0);
于 2021-07-19T16:31:17.050 回答
2

我在TextView内部使用时遇到了这个问题ScrollView。这个解决方案对我有用。

scrollView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                description.getParent().requestDisallowInterceptTouchEvent(false);

                return false;
            }
        });

        description.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                description.getParent().requestDisallowInterceptTouchEvent(true);

                return false;
            }
        });
于 2018-06-06T01:23:18.350 回答
2

下面的代码创建了一个自动水平滚动的文本视图:

在将 TextView 添加到 xml 时使用

<TextView android:maxLines="1" 
          android:ellipsize="marquee"
          android:scrollHorizontally="true"/>

在 onCreate() 中设置 TextView 的以下属性

tv.setSelected(true);
tv.setHorizontallyScrolling(true); 
于 2016-09-24T16:54:46.820 回答
1

maxLines在 xml中放入scrollbarsTextView。

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:maxLines="5" // any number of max line here.
    />

然后在java代码中。

textView.setMovementMethod(new ScrollingMovementMethod());

于 2018-05-13T18:25:21.217 回答
1

像这样使用它

<TextView  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:maxLines = "AN_INTEGER"
    android:scrollbars = "vertical"
/>
于 2017-03-06T05:09:39.377 回答
1

每当您需要使用 ScrollView 作为父级时,您还可以使用 TextView 的滚动移动方法。

并且当您从纵向到横向您的设备时会出现一些问题。(like) 整个页面是可滚动的,但滚动移动方法不起作用。

如果您仍然需要使用 ScrollView 作为父级或滚动移动方法,那么您也可以使用下面的 desc。

如果您没有任何问题,则使用 E​​ditText 而不是 TextView

见下文 :

<EditText
     android:id="@+id/description_text_question"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:background="@null"
     android:editable="false"
     android:cursorVisible="false"
     android:maxLines="6"/>

在这里,EditText 的行为类似于 TextView

您的问题将得到解决

于 2018-07-12T12:48:41.377 回答
0

我为此苦苦挣扎了一个多星期,终于想出了如何使这项工作!

我的问题是一切都会像“块”一样滚动。文本本身是滚动的,但作为一个块而不是逐行滚动。这显然对我不起作用,因为它会切断底部的线条。以前的所有解决方案都不适合我,所以我自己制作了。

这是迄今为止最简单的解决方案:

在包中创建一个名为“PerfectScrollableTextView”的类文件,然后复制并粘贴此代码:

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

public class PerfectScrollableTextView extends TextView {

    public PerfectScrollableTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setVerticalScrollBarEnabled(true);
        setHorizontallyScrolling(false);
    }

    public PerfectScrollableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setVerticalScrollBarEnabled(true);
        setHorizontallyScrolling(false);
    }

    public PerfectScrollableTextView(Context context) {
        super(context);
        setVerticalScrollBarEnabled(true);
        setHorizontallyScrolling(false);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if(focused)
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean focused) {
        if(focused)
            super.onWindowFocusChanged(focused);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}

最后在 XML 中更改您的“TextView”:

从:<TextView

至:<com.your_app_goes_here.PerfectScrollableTextView

于 2014-12-21T18:32:34.050 回答
0

对于垂直水平可滚动的 TextView,其他一些答案会有所帮助,但我需要能够双向滚动。

最终起作用的是一个 ScrollView,里面有一个 Horizo​​ntalScrollView,还有一个 HSV 里面的 TextView。它非常光滑,一次滑动即可轻松地从一侧到另一侧或从上到下移动。它还只允许一次向一个方向滚动,因此在向上或向下滚动时不会出现左右跳跃。

禁用编辑和光标的 EditText 可以工作,但感觉很糟糕。每次滚动尝试都会移动光标,并且即使在大约 100 行的文件中,它也需要多次滑动才能从上到下或从一侧到另一侧。

使用setHorizontallyScrolling(true)可以工作,但是没有类似的方法可以允许垂直滚动,并且据我所知,它在 ScrollView 内不起作用(只是学习,可能是错误的)。

于 2020-08-28T19:50:29.793 回答
0

尝试这个:

android:scrollbars = "vertical"
于 2019-02-16T05:09:10.997 回答
0

XML - 您可以使用android:scrollHorizontally属性

是否允许文本比视图宽(因此可以水平滚动)。

可以是布尔值,例如“true”或“false”。

Prigramacaly -setHorizontallyScrolling(boolean)

于 2018-11-05T11:23:43.887 回答
0

在我的情况下。Constraint Layout.AS 2.3。

代码实现:

YOUR_TEXTVIEW.setMovementMethod(new ScrollingMovementMethod());

XML:

android:scrollbars="vertical"
android:scrollIndicators="right|end"
于 2017-03-23T09:46:44.883 回答
0

这是 JetPack Compose 美妙而快乐的世界中的答案:

LazyColumn {
        item {
            Text(
                text = "My Long Text"
            )
        }
    }
于 2022-02-23T10:14:13.613 回答