在 Android 中附加文本后自动滚动文本视图
Automatic scrolling a text view after appending text in Android
我试图让 textview 的内容在最近附加的文本处自动滚动到底部。我尝试了很多 post 中的很多方法,但似乎没有办法让它工作。我也尝试了不同的 layout_wheight 和宽度参数等等。愚蠢的是,我过去已经做到了并且它有效,但现在使用相同的设置却不是。
这是我最新的 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 一个:
@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一个:
/** 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());
}
希望有人能解开这个谜....
谢谢!
在 textView 元素的 XML 中,我会尝试添加:
android:重力="bottom"
另外,您确定需要在 scrollview 和 textview 中都指定 android:scrollbars="vertical" 吗?此外,我注意到旧工作版本之间的区别在于它不使用 setMovementMethod() - 就我个人而言,我会在没有滚动视图的情况下使用此方法,但由于您已经拥有滚动视图,所以我看不出您需要它的原因。 ..
我试图让 textview 的内容在最近附加的文本处自动滚动到底部。我尝试了很多 post 中的很多方法,但似乎没有办法让它工作。我也尝试了不同的 layout_wheight 和宽度参数等等。愚蠢的是,我过去已经做到了并且它有效,但现在使用相同的设置却不是。 这是我最新的 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 一个:
@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一个:
/** 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());
}
希望有人能解开这个谜.... 谢谢!
在 textView 元素的 XML 中,我会尝试添加:
android:重力="bottom"
另外,您确定需要在 scrollview 和 textview 中都指定 android:scrollbars="vertical" 吗?此外,我注意到旧工作版本之间的区别在于它不使用 setMovementMethod() - 就我个人而言,我会在没有滚动视图的情况下使用此方法,但由于您已经拥有滚动视图,所以我看不出您需要它的原因。 ..