我正在尝试在Android应用程序中进行自动滚动。
假设,我有这样的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="show me the texts"
android:id="@+id/button"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/text"
android:id="@+id/textView1"/>
</ScrollView>
</LinearLayout>
在此xml代码中,我在屏幕顶部有一个Button,并有一个由ScrollView包裹的TextView。这意味着我的TextView现在可以滚动。
它可以正常工作,因为当用户触摸屏幕并向下或向上交换时,可以上下滚动TextView。
但...
我打算有一个Auto Scrollable TextView,我的意思是我需要一个可滚动的TextView,它在10秒后向下滚动约10dp。
我知道我必须在我的应用程序中使用一些线程来计算时间,但是例如,我不知道如何说10秒钟后TextView向下滚动。
有什么建议?任何提示将不胜感激。谢谢
您可以使用以下方法:
mScrollView.post(new Runnable() {
public void run() {
mScrollView.smoothScrollBy(10, 10);
}
});
smoothScrollBy的文档:http : //developer.android.com/reference/android/widget/ScrollView.html#smoothScrollBy (int,int )
要每10秒执行一次此任务:
TimerTask fileProcessTask = new TimerTask(){
@Override
public void run() {
//Scrolling here
}
};
Timer tm = new Timer();
tm.schedule(fileProcessTask, 10000L);
还可以看看此SO Post:https : //stackoverflow.com/a/14685729/3326331,我还没有尝试过,但是您可以尝试一下。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句