OuNuo0 2013-08-05
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/sv_menu" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/notifacation_background" android:scrollbars="none" > <LinearLayout android:id="@+id/ll_tt" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" > <TextView android:id="@+id/tv_01" android:layout_width="250dip" android:layout_height="250dip" android:layout_marginTop="10px" android:background="@drawable/notifacation_background" android:text="一去二三里" android:textColor="#000000" android:textSize="22sp" android:textStyle="bold" android:gravity="center"/> <TextView android:layout_width="250dip" android:layout_height="250dip" android:layout_marginTop="10dip" android:background="@drawable/notifacation_background" android:text="烟村四五家" android:textColor="#000000" android:textSize="22sp" android:textStyle="bold" android:gravity="center" /> <TextView android:layout_width="250dip" android:layout_height="250dip" android:layout_marginTop="10px" android:background="@drawable/notifacation_background" android:text="亭台六七座" android:textColor="#000000" android:textSize="22sp" android:textStyle="bold" android:gravity="center" /> <TextView android:layout_width="250dip" android:layout_height="250dip" android:layout_marginTop="10px" android:background="@drawable/notifacation_background" android:text="八九十枝花" android:textColor="#000000" android:textSize="22sp" android:textStyle="bold" android:gravity="center" /> </LinearLayout> </ScrollView>很简单, 不过写的不是很规范。。。
import android.app.Activity; import android.os.Bundle; import android.os.CountDownTimer; import android.view.ViewGroup; import android.view.ViewTreeObserver.OnPreDrawListener; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; public class AActivity extends Activity { private ScrollView svMenu; //需要滚动 private TextView tv01; //我们要用到它的高度信息 private boolean isScroll = false; // OnPreDrawListener是多次调用的 只要拖动都会有调用 所以得到值后加标记 private int scrollY = 0; // 不用多说了 滚动的距离 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.new_activity); svMenu = (ScrollView) findViewById(R.id.sv_menu); tv01 = (TextView) findViewById(R.id.tv_01); //注册监听器 tv01.getViewTreeObserver().addOnPreDrawListener(preDrawListener); } private OnPreDrawListener preDrawListener = new OnPreDrawListener() { @Override public boolean onPreDraw() { if (!isScroll) { isScroll = true; // 这里我有margin 所以得到margin ViewGroup.LayoutParams p = tv01.getLayoutParams(); if (p != null && p instanceof LinearLayout.LayoutParams) { LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) p; scrollY = tv01.getHeight() + lp.topMargin; } System.out.println("高度: " + scrollY); // 这个timer执行scrollY毫秒 每1毫秒回调一次 new CountDownTimer(scrollY, 1) { public void onTick(long millisUntilFinished) { System.out.println("finished: " + millisUntilFinished); svMenu.scrollTo(0, (int) (scrollY - millisUntilFinished)); } public void onFinish() { System.out.println("done!"); svMenu.scrollTo(0, scrollY); } }.start(); // remove 必须重新得到 之前用了ViewTreeObserver的对象 remove会报错 tv01.getViewTreeObserver().removeOnPreDrawListener( preDrawListener); } return true; } }; }源地址:http://751401909.iteye.com/admin/blogs/1920553