auto-scrolling TextView in android to bring text into view

前端 未结 13 1701

I have a TextView that I\'m dynamically adding text to.

in my main.xml file I have the properties set to make my max lines 19 and scrollbar

相关标签:
13条回答
  • 2020-11-28 05:33

    I used a little trick ... in my case....

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/imageButton">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scrollView"
            android:layout_gravity="left|top" >
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textMultiLine"
                android:ems="10"
                android:text="@string/your_text" />
        </ScrollView>
    
    </FrameLayout>
    
    0 讨论(0)
  • 2020-11-28 05:35

    https://stackoverflow.com/a/7350267/4411645 didn't exactly work for me for

    1. the getLayout can throw NPE when the text is recently changed.
    2. scrollTo should be changed to scrollBy
    3. It doesn't take relative position of the textview or paddings into account.

    Needless to say it is a good starting point. Here's my variation of the implementation, within a textwatcher. We have to subtract the textView top from the bottom when calculating the delta because getLineTop() also returns the value relative to the textView top.

            @Override
            public void afterTextChanged(Editable editable) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Layout layout = textView.getLayout();
                        if (layout != null) {
                            int lineTop = layout.getLineTop(textView.getLineCount());
                            final int scrollAmount = lineTop + textView.getPaddingTop()
                                    + textView.getPaddingBottom() - textView.getBottom() + textView.getTop();
                            if (scrollAmount > 0) {
                                textView.scrollBy(0, scrollAmount);
                            } else {
                                textView.scrollTo(0, 0);
                            }
                        }
                    }
                }, 1000L);
            }
    

    You can play around with the delay to better your ux.

    0 讨论(0)
  • 2020-11-28 05:36

    Simple implementation...in your XML layout define your TextView with these attributes:

    <TextView
        ...
        android:gravity="bottom"
        android:scrollbars="vertical"
    />
    
    0 讨论(0)
  • 2020-11-28 05:39
     scrollview=(ScrollView)findViewById(R.id.scrollview1); 
     tb2.setTextSize(30); 
     tb2=(TextView)findViewById(R.id.textView2);                 
     scrollview.fullScroll(View.FOCUS_DOWN);    
    

    Or use this in TextView:

    <TextView
    
    android:id="@+id/tb2"
    android:layout_width="fill_parent"
    android:layout_height="225sp"
    android:gravity="top"
    android:background="@android:drawable/editbox_background"
    android:scrollbars="vertical"/>
    
    0 讨论(0)
  • 2020-11-28 05:40

    (2017) using Kotlin:

    // you need this to enable scrolling:
    mTextView.movementMethod = ScrollingMovementMethod()
    // to enable horizontal scrolling, that means word wrapping off:
    mTextView.setHorizontallyScrolling(true)
    ...
    mTextView.text = "Some long long very long text content"
    mTextView.post {
         val scrollAmount = mTextView.layout.getLineTop(mTextView.lineCount) - mTextView.height
         mTextView.scrollTo(0, scrollAmount)
    }
    

    This works file for me

    0 讨论(0)
  • 2020-11-28 05:42

    Took some digging through the TextView source but here's what I came up with. It doesn't require you to wrap the TextView in a ScrollView and, as far as I can tell, works perfectly.

    // function to append a string to a TextView as a new line
    // and scroll to the bottom if needed
    private void addMessage(String msg) {
        // append the new string
        mTextView.append(msg + "\n");
        // find the amount we need to scroll.  This works by
        // asking the TextView's internal layout for the position
        // of the final line and then subtracting the TextView's height
        final int scrollAmount = mTextView.getLayout().getLineTop(mTextView.getLineCount()) - mTextView.getHeight();
        // if there is no need to scroll, scrollAmount will be <=0
        if (scrollAmount > 0)
            mTextView.scrollTo(0, scrollAmount);
        else
            mTextView.scrollTo(0, 0);
    }
    

    Please let me know if you find a case where this fails. I'd appreciate being able to fix any bugs in my app ;)

    Edit: I should mention that I also use

    mTextView.setMovementMethod(new ScrollingMovementMethod());
    

    after instantiating my TextView.

    0 讨论(0)
提交回复
热议问题