编写长文本在Android屏幕上显示以便屏幕滚动显示

问题描述:

我想显示长度为2-3页的文本,尝试使用滚动视图但文本被截断,我是新开发人员,请给予简单的例子,thanx编写长文本在Android屏幕上显示以便屏幕滚动显示

+1

请发布您的XML布局文件.. – Venkatesh 2012-07-21 05:15:58

+0

http://www.helloandroid.com/tutorials/how-draw-multiline-text-canvas-easily我认为这个链接可以帮助你更好。 – Venkatesh 2012-07-21 05:30:22

这将工作,诀窍是让你想滚动内滚动视图。

<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> 
+0

thanx这是最简单的 – dashh 2012-07-21 13:35:03

文本切断意味着你遇到了什么问题。尝试下面的代码。它的工作原理.....

使用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> 

如果要创建动态是指,按照这个代码: 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); 

} 

改变你的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> 

试试这个代码。它的作品....

+0

thanx的代码,但我做了同样的事情,但当文本增加屏幕大小滚动应该是他们向下滚动以查看其余文本,为什么它不是happen'n,pl指导我我是新的发展 – dashh 2012-07-21 12:51:06

在你的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" 
/> 

而且在Activity,这样写:

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

这将滚动文本TextView。无需写入ScrollView

+2

我想知道为什么大家都在scrollview中推荐textview时,textview可以管理自己的滚动:P – Shubhayu 2012-07-21 05:49:56

+0

@Shubhayu:非常感谢...... :) – 2012-07-21 05:51:55

+0

这是正确的方式来显示大文本。 – Chandu 2017-05-02 18:02:33