<ScrollView
android:layout_width="fill_parent"
android:layout_height="100dip"
android:id="@+id/scrollView"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:text="@string/hello"
/>
</ScrollView>
可是,我的ScrollView高度比较小,手动滑动的话似乎不方便,那么就不手动滑动让它自己滑动吧。。。
如下:
xml中:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="100dip"
android:id="@+id/scrollView"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:text="@string/hello"
/>
</ScrollView>
java代码中:
public class TestScrollViewActivity extends Activity {
TextView textView ;
ScrollView scrollView;
Handler mHandler ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler = new Handler();
textView = (TextView) this.findViewById(R.id.textView);
scrollView = (ScrollView) this.findViewById(R.id.scrollView);
Thread t = new Thread(new Runnable(){
public void run() {
int flag = 0;
try {
Thread.sleep(1000);
while (flag < 3){
if(scrollView.getScrollY() < (textView.getHeight() - scrollView.getHeight())){
System.out.println("");
System.out.println("scrollView.getScrollY() ====== " + scrollView.getScrollY());
mHandler.post(new Runnable(){
public void run() {
scrollView.smoothScrollBy(0, textView.getLineHeight()/10);
}
});
Thread.sleep(100);
}else{
mHandler.post(new Runnable(){
public void run() {
scrollView.smoothScrollTo(0, 0);
System.out.println("--- else ---------");
}
});
Thread.sleep(1000);
flag ++ ;
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
t.start();
}
}