一. 继承图

ScrollView称为滚动视图,当在一个屏幕的像素显示不下绘制的UI控件时,可以采用滑动的方式,使控件显示。
从继承图可以看出,ScrollView原来是一个FrameLayout的容器,不过在他的基础上添加了滚动,允许显示的比实际多的内容。
1.首先,我们在一个垂直排列的 LinearLayout 的 放置五个按钮,我们会发现由于超出屏幕的显示范围,我们看不到下面部分的按钮。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical">
<Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="20dp" android:gravity="center" android:text="内容一" android:textColor="#03A9F4" android:textSize="24sp"/>
<Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="80dp" android:gravity="center" android:text="内容二" android:textColor="#03A9F4" android:textSize="24sp"/>
<Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="80dp" android:gravity="center" android:text="内容三" android:textColor="#03A9F4" android:textSize="24sp"/>
<Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="80dp" android:gravity="center" android:text="内容四" android:textColor="#03A9F4" android:textSize="24sp"/>
<Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="80dp" android:gravity="center" android:text="内容五" android:textColor="#03A9F4" android:textSize="24sp"/>
</LinearLayout>
|

2.我们可以使用ScrollView让界面可以滚动,这样就可以滚动而看到下面的内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="20dp" android:gravity="center" android:text="内容一" android:textColor="#03A9F4" android:textSize="24sp"/>
<Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="80dp" android:gravity="center" android:text="内容二" android:textColor="#03A9F4" android:textSize="24sp"/>
<Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="80dp" android:gravity="center" android:text="内容三" android:textColor="#03A9F4" android:textSize="24sp"/>
<Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="80dp" android:gravity="center" android:text="内容四" android:textColor="#03A9F4" android:textSize="24sp"/>
<Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="80dp" android:gravity="center" android:text="内容五" android:textColor="#03A9F4" android:textSize="24sp"/>
</LinearLayout>
</ScrollView>
|
3.此时,我们需要注意,ScrollView的子元素只能有一个,可以是一个View(如ImageView、TextView等) 也可以是一个ViewGroup(如LinearLayout、RelativeLayout等),其子元素内部则不再限制,否则会报以下异常。
1
| Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
|
在实际使用时,我们也会遇到水平方向,控件超出屏幕的情况。这时就需要使用水平方向的滚动视图HorizontalScrollView。
和 ScrollView 的用法一致,我们在上面的例子上简单的修改一下成为一个新的例子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| <?xml version="1.0" encoding="utf-8"?> <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">
<Button android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="80dp" android:gravity="center" android:text="内容一" android:textColor="#03A9F4" android:textSize="18sp"/>
<Button android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="80dp" android:gravity="center" android:text="内容二" android:textColor="#03A9F4" android:textSize="18sp"/>
<Button android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="80dp" android:gravity="center" android:text="内容三" android:textColor="#03A9F4" android:textSize="18sp"/>
<Button android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="80dp" android:gravity="center" android:text="内容四" android:textColor="#03A9F4" android:textSize="18sp"/>
<Button android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="80dp" android:gravity="center" android:text="内容五" android:textColor="#03A9F4" android:textSize="18sp"/>
</LinearLayout>
</HorizontalScrollView>
|

四. Xml中常用的属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
android:fadingEdge=""
android:overScrollMode="never"
android:scrollbars=""
android:scrollbarSize=""
android:scrollbarStyle=""
android:descendantFocusability=""
android:fillViewport=""
|
五. 常用的方法
1. 滑动开关控制
1 2 3 4 5 6 7
| scrollView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { return true; } });
|
2.滑动位置控制
1 2 3 4 5 6 7 8 9 10
| scrollView.post(new Runnable() { @Override public void run() { scrollView.fullScroll(ScrollView.FOCUS_UP); scrollView.fullScroll(ScrollView.FOCUS_DOWN); } });
|
3. 滑动到某个位置
1 2 3 4 5 6 7 8
| scrollView.post(new Runnable() { @Override public void run() { int offset = 100; scrollView.smoothScrollTo(0, offset); } });
|
4. 滚动监听
基本的 setOnScrollListener() 的方法系统并没有帮我们实现,我们可以使用继承的方式自定义控件。
参考:Android 中实现 ScrollView 的滚动事件监听
参考文章
让你的布局滚动起来—ScrollView
[Android的ScrollView简单使用实例(附Demo)](https://blog.csdn.net/qq_36243942/article/details/82185051# 1.垂直滚动:Scroll)