我正在构建一个类似聊天的应用程序,它使用 ScrollView 在屏幕上显示用户输入的文本。我想做的是随着更多文本附加到屏幕上,使 ScrollView 自动滚动。 我正在使用 TextView 来显示输入。 ScrollView 的xml部分如下:
<ScrollView
android:id="@+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/buttons"
>
<LinearLayout
android:id="@+id/start_chat"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
我该如何去做呢?我尝试将布局的重力设置为“底部”,但这无法正常工作(随着 ScrollView 向下滚动而向上移动的输入文本无法再次查看)。
非常感谢任何帮助。
最佳答案
如果您想滚动到绝对底部(添加新 TextView 的位置),请使用 fullScroll添加 TextView 时在 ScrollView 上。
ScrollView my_scrollview = (ScrollView) findViewById(R.id.scroller);
my_scrollview.fullScroll(ScrollView.FOCUS_DOWN);
如果您想从用户正在查看的当前位置相对滚动,我会尝试类似 smoothScrollBy 的内容或scrollBy
/* recently_added_textview being the TextView that was added to trigger this */
ScrollView my_scrollview = (ScrollView) findViewById(R.id.scroller);
int height = recently_added_textview.getHeight();
my_scrollview.smoothScrollBy(0, height);
/* x being 0 assuming you don't want to scroll left and right */
这样,如果用户正在阅读顶部或中间的内容,他们就不会被发送到 ScrollView 的底部。
关于java - Android 中的自动滚动 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14760033/