简体   繁体   中英

How to make textView scroll vertically automatically?

I want my textView to start automatically scrolling (vertically) when my activity starts I am displaying a paragraph in a small textView. The textView is manually scrollable vertically but I want it to do this on its own.

I added this attribute but it did not help. android:gravity = "bottom"

Thanks

<TextView
        android:id="@+id/announcementNewsText"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:autoSizeTextType="uniform"
        android:scrollbars="vertical"
        android:textColor="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        app:layout_constraintVertical_bias="1.0"


        />

Add scroll view in your XML like this

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
            android:id="@+id/announcementNewsText"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:autoSizeTextType="uniform"
            android:scrollbars="vertical"
            android:textColor="#FFFFFF"/>

        </RelativeLayout>

    </ScrollView>

And in java onCreate() find the scroll view

    ScrollView s = findViewById(R.id.scroll_view);
         s.setSmoothScrollingEnabled(true);
         s.post(new Runnable() {            
    @Override
    public void run() {
           s.fullScroll(View.FOCUS_DOWN);              
    }
});

Add

android:scrollbars = "vertical"

to your textView and use

yourTextView.setMovementMethod(new ScrollingMovementMethod());

in your code.

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