Android开发学习教程(20)- 滚动控件ScrollView的用法

104 阅读1分钟

—— 与其去讨好别人,不如武装自己,与其去听风听雨,不如昂首的走出去。

ScrollView是什么

ScrollView是一种特殊的布局。当ScrollView嵌套的内容大于他本身的size的时候,ScrollView会自动添加滚动条,并可以竖直滑动。

ScrollView有什么用

当ScrollView嵌套的内容大于他本身的size的时候,ScrollView会自动添加滚动条,并可以竖直滑动。

ScrollView怎么用

  1. 布局中加入ScrollView控件
12345678910111213141516171819202122232425262728<ScrollView``    ``android:layout_width=``"match_parent"``    ``android:layout_height=``"wrap_content"``>``    ``<LinearLayout``        ``android:layout_width=``"match_parent"``        ``android:layout_height=``"wrap_content"``        ``android:orientation=``"vertical"``>``        ``<TextView``            ``android:layout_width=``"match_parent"``            ``android:layout_height=``"300dp"``            ``android:text=``"textview1" />``        ``<TextView``            ``android:layout_width=``"match_parent"``            ``android:layout_height=``"300dp"``            ``android:text=``"textview2" />``        ``<TextView``            ``android:layout_width=``"match_parent"``            ``android:layout_height=``"300dp"``            ``android:text=``"textview3" />``        ``<TextView``            ``android:layout_width=``"match_parent"``            ``android:layout_height=``"300dp"``            ``android:text=``"textview4" />``    ``</LinearLayout>``</ScrollView>

运行效果:

Android开发学习教程(20)- 滚动控件ScrollView的用法

  1. ScrollView的直接子View只能有一个。也就是说如果你要使用很复杂的视图结构,必须把这些视图放在一个标准布局里,如LinearLayout、RelativeLayout等。

  2. 你可以使用layout_width和layout_height给ScrollView指定大小。

  3. ScrollView只用来处理需要滚动的不规则视图的组合。大批量的列表数据展示可以使用ListView、GridView或者RecyclerView。

  4. ScrollView和ListView之类的嵌套使用时会有滑动冲突。不到不得已不要使用。

  5. ScrollView只支持竖直滑动,水平滑动使用HorizontalScrollView。

  6. ScrollView的android:fillViewport属性定义了是否可以拉伸其内容来填满viewport。你可以可以调用方法setFillViewport(boolean)来达到一样的效果。

**