编写长文本以在 android 屏幕上显示,以便屏幕向下滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11589652/
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 08:09:53  来源:igfitidea点击:

writing long text to display on screen in android so that screen scoll down

android

提问by dashh

I want to display text which is 2-3 pages long ,tried to use scroll view but text get cut off,i'm new in development,kindly give simple example,thanx

我想显示 2-3 页长的文本,尝试使用滚动视图但文本被切断,我是开发新手,请举个简单的例子,谢谢

回答by Krishna Suthar

In your XML, write this TextView:

在您的 XML 中,编写此 TextView:

<TextView
        android:id="@+id/txtDetails"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="7"
        android:scrollbars="vertical"
        android:text="TextView"
 />

And in Activity, write this:

在 中Activity,写下这个:

TextView txtDetails = (TextView) findViewById(R.id.txtDetails);
txtDetails.setText("Your Text");
txtDetails.setMovementMethod(new ScrollingMovementMethod());

This will scroll the text in TextView. No need to write in ScrollView.

这将滚动 中的文本TextView。不需要写进去ScrollView

回答by Krishna Suthar

Text getting cut off means what is the problem you are getting. Try the following code. It works.....

文本被截断意味着您遇到的问题是什么。试试下面的代码。有用.....

using xml code:

使用xml代码:

  <?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="HERE I TOOK NEARLY 3-4 PAGES OF DOCUMENT. IT WORKED" />

</ScrollView>

If you are creating dynamically means, follow this code: main.java:

如果您是动态创建方式,请遵循以下代码:main.java:

oncreate()
{
    ScrollView sv=(ScrollView)findViewById(R.id.scrollView1);
    TextView tv=new TextView(this);
    tv.setText("just keep the text what you want here");
    sv.addView(tv);

}

change your xml as the following one:

将您的 xml 更改为以下内容:

main.xml

主文件

  <?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" > 
</ScrollView>

Try this code. it works....

试试这个代码。有用....

回答by hatcyl

This will work, the trick is to get what you want to scroll inside the scrollview.

这会起作用,诀窍是在滚动视图中获取您想要滚动的内容。

<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" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/text" />

        </LinearLayout>
    </ScrollView>

</RelativeLayout>