3

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

4

3 回答 3

11

在您的 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

于 2012-07-21T05:45:44.433 回答
4

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

<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>
于 2012-07-21T05:42:41.003 回答
4

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

使用 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 更改为以下内容:

主要的.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>

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

于 2012-07-21T05:29:44.097 回答