在Android中附加文本后自动滚动文本视图

时间:2015-07-17 15:09:50

标签: java android textview vertical-scrolling

我试图让textview的内容自动滚动到附加的最新文本的底部。我从很多帖子尝试了很多方法,但似乎没有办法让它发挥作用。我还有不同的layout_wheight和width参数等等。愚蠢的事情是,我已经在过去制作它并且它有效,但现在使用相同的设置它不是。 这是我最新的xml代码:

    <TableRow
    android:id="@+id/tableRow1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1.5"
    android:weightSum="2">

    <ScrollView
        android:id="@+id/askScv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="vertical">

        <TextView
            android:id="@+id/askTxv"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:maxLines="99999"
            android:scrollbars="vertical"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </ScrollView>

    <ScrollView
        android:id="@+id/answerScv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="vertical">


        <TextView
            android:id="@+id/answerTxv"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:maxLines="99999"
            android:scrollbars="vertical"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </ScrollView>
</TableRow>

和Java one:

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mQuestionEdt = (EditText) findViewById(R.id.questionEdt);

    mAnswerTxv = (TextView) findViewById(R.id.answerTxv);
    mAnswerTxv.setMovementMethod(new ScrollingMovementMethod());

    mAnswerScv=(ScrollView)findViewById(R.id.answerScv);
    mAnswerScv.fullScroll(View.FOCUS_DOWN);

    mAskTxv=(TextView) findViewById(R.id.askTxv);
    mAskTxv.setMovementMethod(new ScrollingMovementMethod());

    mAskScv=(ScrollView)findViewById(R.id.askScv);
    mAskScv.fullScroll(View.FOCUS_DOWN);

    mAskBtn = (ImageButton) findViewById(R.id.askBtn);
    mAskBtn.setOnClickListener(this);

    mStartBtn = (ImageButton) findViewById(R.id.startBtn);
    mStartBtn.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    Bundle result;

    if (view.equals(mAskBtn)) {
        result=mAnsweringMachine.answer(mQuestionEdt.getText().toString());
        mAskTxv.append("\n" + mQuestionEdt.getText().toString());
        mAskScv.smoothScrollTo(0,mAskTxv.getBottom());
        mAnswerTxv.append("\n" + result.getString(AnsweringMachine.DIALOGUE_RESULT));
        mAnswerScv.smoothScrollTo(0,mAnswerTxv.getBottom());
    } else if (view.equals(mStartBtn)) {

        result = mAnsweringMachine.runStartingPrompt();
        mAnswerTxv.append(result.getString(AnsweringMachine.DIALOGUE_RESULT));
        //mAnswerScv.fullScroll(View.FOCUS_DOWN);
    }

}

这是我以前的工作代码,xml:

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

    <TextView
        android:id="@+id/terminalTxv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</ScrollView>

和Java one:

    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_terminal);
    terminalTxv=(TextView) findViewById(R.id.terminalTxv);
    terminalScv=(ScrollView) findViewById(R.id.terminalScv);
    terminalScv.fullScroll(View.FOCUS_DOWN);
}

@Override
protected void onBtDataReceived(int[] data) {
    super.onBtDataReceived(data);
    String out=String.valueOf(terminalTxv.getLineCount())+" - ";
    for (int x=0;x<data.length-1;x++){
        out+=data[x];
        out+=" ";
    }
    out+="\n";
    terminalTxv.append(out);
    terminalScv.smoothScrollTo(0, terminalTxv.getBottom());
}

希望有人能让我理解这个谜...... 谢谢!

1 个答案:

答案 0 :(得分:0)

在textView元素的XML中,我会尝试添加:

机器人:比重=&#34;底部&#34;

你还确定你需要指定android:scrollbars =&#34; vertical&#34;在scrollview和textview中?另外,我注意到旧工作版本之间的区别在于它没有使用setMovementMethod() - 我个人会在没有scrollview的情况下使用这种方法,但是因为你已经有scroollview我不明白为什么会这样做你需要它......

相关问题