Android アプリにテキストビューを含むスクロールビューがあります。このテキストビューには、設定された間隔で継続的にテキストが追加されます。スクロールは機能し、テキストは問題なく追加されますが、私がやりたいのは、テキストが追加されたときにスクロールビューを自動スクロールさせることです。新しいテキストが下部に追加されると、一致するように自動的に下にスクロールし、古いテキストは上部に押し出されて見えなくなります。さらに良いのは、スクロールビューの下部にテキストを追加し、古いテキストを上に押し上げることですが、一度に 1 つずつです。
32827 次
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
重力で解決策を試しましたが、下にスクロールすると、実際のテキストの下に多くの空白ができました。
それは別の方法です。ScrollView 内に TextView を配置できます。
<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 に答える