简体   繁体   中英

TextView won't scroll

I am currently working in an android activity where I need a scroll-able dynamic text view. I am constantly adding new lines to this view, but as I add lines, to it, it will only go up to 4 lines (The number I set it too) then it won't scroll. This is my xml and relevant java code:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="4"
    android:scrollbars="vertical"
    android:id="@+id/textView2"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

Java:

 protected void onCreate(Bundle savedInstanceState) {
    ...
    RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
    tv3 = (TextView) findViewById(R.id.textView2);
    tv3.setMovementMethod(new ScrollingMovementMethod());

It won't scroll unless you put it in a scroll view. And if you specify the maxLines it wont exceed that. So instead, don't specify max lines and add a height to your scroll view.

<FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="120dp"> //or whatever height

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"> 

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            android:layout_centerHorizontal="true" />

    </ScrollView>

</FrameLayout>

Try this one.

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

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

          <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="4"
            android:scrollbars="vertical"
            android:id="@+id/textView2"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />

     </LinearLayout>

    </ScrollView>

我只是将其放入scrollView内,然后将scrollView的高度设置为android:height="wrap_content"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM