24

我在 Android 应用程序中有一个包含 textview 的滚动视图。此文本视图将以设定的时间间隔连续附加文本。滚动工作并且文本添加得很好,但我想做的是在添加文本时让滚动视图自动向下滚动。当新文本附加在底部时,它会自动向下滚动以匹配,旧文本在顶部被推到视线之外。更好的是在滚动视图的底部添加文本并让它向上推动旧文本,但一次只做一件事。

4

5 回答 5

18
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    >
    <TextView
        android:id="@+id/statusText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" />
</ScrollView>
于 2011-07-06T21:14:13.463 回答
9

我已经尝试了重力解决方案,但结果是滚动到底部时实际文本下有很多空白空间。

所以这是另一种方式。您可以将 TextView 放在 ScrollView 内:

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:fillViewport="true" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</ScrollView>

并定义 addTextChangedListener

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

            //some code here

        ScrollView scrollView1 = (ScrollView) findViewById(R.id.scrollView1);
        TextView textView1 = (TextView) findViewById(R.id.textView1);
        textView1.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {
            scrollView1.fullScroll(ScrollView.FOCUS_DOWN);
            // you can add a toast or whatever you want here
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            //override stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            //override stub
        }

    }) }

实际上我现在注意到每次更改文本时应用程序都会滚动,而不仅仅是添加。但是它对我来说很好。

于 2013-11-08T13:57:53.010 回答
3

添加文本时,您可以简单地将整个滚动ScrollView到底部。

textView.append(text);
scrollView.fullScroll(View.FOCUS_DOWN);

正如@RaphaelRoyer-Rivard 在他的评论中建议的那样,您可以通过以下方式获得更可靠的结果post

textView.append(text);
scrollView.post(() -> scrollView.fullScroll(View.FOCUS_DOWN));
于 2013-02-11T09:24:55.173 回答
0

如果您正在寻找一种更通用的自动滚动到 ScrollView 底部的方法,您可以尝试下面的代码片段。它的优点是您不需要发布 Runnable 来确保您在 UI 线程中。它的缺点是我不确定这会破坏什么。

public class AutoScroller extends ScrollView {
    public boolean autoscroll = true;

    public AutoScroller(Context context) {super(context);}
    public AutoScroller(Context context, AttributeSet attrs) {super(context,attrs);}
    public AutoScroller(Context context, AttributeSet attrs, int defStyleAttr){super(context, attrs, defStyleAttr);}
    public AutoScroller(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);}


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(autoscroll) {
           scrollTo(getScrollX(), getChildAt(getChildCount()-1).getBottom() - getHeight());
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

另一个要覆盖的候选人可能是onLayout.

于 2019-11-12T16:17:22.733 回答
-1
textView.append(text);
scroll.fullScroll(View.FOCUS_DOWN)

这会产生问题,它不会滚动到极端顶部,因为android:layout_gravity="bottom"

于 2013-03-14T06:39:46.173 回答