使 TextView 在 Android 上可滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1748977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:35:01  来源:igfitidea点击:

Making TextView scrollable on Android

androidscrolltextview

提问by Muhammad Maqsoodur Rehman

I am displaying text in a textview that appears to be too long to fit into one screen. I need to make my TextView scrollable. How can I do that?

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

Here is the code:

这是代码:

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

回答by Amit Chintawar

You don't need to use a ScrollViewactually.

你不需要使用一个ScrollView实际的。

Just set the

只需设置

android:scrollbars = "vertical"

properties of your TextViewin your layout's xml file.

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

Then use:

然后使用:

yourTextView.setMovementMethod(new ScrollingMovementMethod());

yourTextView.setMovementMethod(new ScrollingMovementMethod());

in your code.

在你的代码中。

Bingo, it scrolls!

宾果游戏,它滚动!

回答by Someone Somewhere

This is how I did it purely in XML:

这就是我纯粹在 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>

NOTES:

笔记:

  1. android:fillViewport="true"combined with android:layout_weight="1.0"will make the textview take up all available space.

  2. When defining the Scrollview, DO NOT specify android:layout_height="fill_parent"otherwise the scrollview doesn't work! (this has caused me to waste an hour just now! FFS).

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

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

PRO TIP:

专家提示:

To programmatically scroll to the bottom after appending text, use this:

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

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

回答by EddieB

All that is really necessary is the setMovementMethod(). Here's an example using a LinearLayout.

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

File main.xml

文件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>

File WordExtractTest.java

文件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);
    }
}

回答by matasoy

Make your textview just adding this

让你的 textview 只添加这个

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

回答by Ahsan Raza

It is not necessary to put in

没有必要放入

android:Maxlines="AN_INTEGER"`

You can do your work by simply adding:

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

android:scrollbars = "vertical"

And, put this code in your Java class:

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

textview.setMovementMethod(new ScrollingMovementMethod());

回答by Samuh

You can either

你可以

  1. surround the TextViewby a ScrollView; or
  2. set the Movement method to ScrollingMovementMethod.getInstance();.
  1. 围绕TextView由一个ScrollView; 或者
  2. 将移动方法设置为ScrollingMovementMethod.getInstance();

回答by Valentin Galea

The best way I found:

我发现的最好方法:

Replace the TextView with an EditText with these extra attributes:

用带有这些额外属性的 EditText 替换 TextView:

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

There is no need for wrapping in a ScrollView.

不需要包装在 ScrollView 中。

回答by MBH

Simple. This is how I did it:

简单的。我是这样做的:

  1. XML Side:

    <?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 side:

    tv_log = (TextView) findViewById(R.id.tv_log);
    tv_log.setMovementMethod(new ScrollingMovementMethod());
    
  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());
    

Bonus:

奖金:

To let the text view scroll down as the text fill it, you have to add:

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

    android:gravity="bottom"

to the TextView xml file. It will scroll down automatically as more text comes in.

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

Of course you need to add the text using the append function instead of set text:

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

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

I used it for Log purpose.

我将它用于日志目的。

I hope this helps ;)

我希望这有帮助 ;)

回答by mOmO

This is "How to apply ScrollBar to your TextView", using only XML.

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

First, you need to take a Textview control in the main.xmlfile and write some text into it ... like this:

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

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

Next, place the text view control in between the scrollview to display the scroll bar for this 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>

That's it...

就是这样...

回答by Mark Cramer

The "pro tip" above from Someone Somewhere (Making TextView scrollable on Android) works great, however, what if you're dynamically adding text to the ScrollView and would like to automatically scroll to the bottom after an append onlywhen the user is at the bottom of the ScrollView? (Perhaps because if the user has scrolled up to read something you don't want to automatically reset to the bottom during an append, which would be annoying.)

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

Anyway, here it is:

无论如何,这里是:

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

The mTextStatus.getLineHeight() will make it so that you don't scrollToBottom() if the user is within one line from the end of the ScrollView.

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