添加Android scrollview自动滚动作为文本

And*_*ful 23 android scrollview textview autoscroll

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

pap*_*tis 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>
Run Code Online (Sandbox Code Playgroud)


Kam*_*ado 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>
Run Code Online (Sandbox Code Playgroud)

并定义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
        }

    }) }
Run Code Online (Sandbox Code Playgroud)

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