您的位置:首页 > 其它

ScrollView实现TextView一行一行的滚动效果

2012-12-18 10:59 204 查看
在项目中因为是混合布局,TextView的宽度比较窄而内容又很多,这样用跑马灯似乎就不很合适了。。。于是,想到要一行一行的滚动TextView,这个当然要用ScrollView了,代码如下:
<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();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: