Android ScrollView

ScrollView 滚动视图

滚动视图用于为其它组件添加滚动条,在默认的情况下,当窗体中内容比较多,而一屏显示不下时,
超出的部分不能被用户所看到.因为Android的布局管理器本身没有提供滚动屏幕的功能.如果
要让其滚动,就要使用滚动视图ScrllView.

滚动视图是FrameLayout的子类,因此,在滚动视图中,可以添加任何想要放入其中的组件,但是一
个滚动视图中只能放一个组件,如果要放置多个,可以先放一个存布局管理器.再将要放置的组件
放置到该布局管理器中,在滚动视图中,使用比较多的是线性布局管理器.

 1 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:id="@+id/scrollview"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:scrollbars="none" >
 6 
 7     <LinearLayout
 8         android:id="@+id/ll"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:orientation="vertical" >
12          
13     </LinearLayout>
14 
15 </ScrollView>
activity_main.xml
 1 public class MainActivity extends Activity {
 2 
 3     ScrollView view;
 4     LinearLayout ll;
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9         
10         view = (ScrollView) findViewById(R.id.scrollview);
11         ll = (LinearLayout) findViewById(R.id.ll);
12         for (int i = 0; i < 20; i++) {
13             ImageView img = new ImageView(this);
14             img.setPadding(20, 20, 20, 20);
15             img.setImageResource(R.drawable.ic_launcher);
16             ll.addView(img);
17         }
18         
19         //滑动到指定位置
20         int width = view.getWidth();
21         Log.e("TAG", width+"");
22         final long startTime = System.currentTimeMillis();
23         
24         //这个方法必须等待view完全显示,post延迟操作
25         view.post(new Runnable() {
26             
27             @Override
28             public void run() {
29                 int width = view.getWidth();
30                 Log.e("TAG", width+":"+(System.currentTimeMillis()-startTime));
31                 //滾到底部
32                 view.fullScroll(ScrollView.FOCUS_DOWN);
33             }
34         });
35         
36     }
37 }
MainActivity.java

原文地址:https://www.cnblogs.com/Claire6649/p/5964895.html