您当前的位置:首页 > Android教程

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

时间:2022-01-16 07:12:34 阅读数:20,854人阅读
版权声明:转载请注明出处,谢谢!
—— 与其去讨好别人,不如武装自己,与其去听风听雨,不如昂首的走出去。

ScrollView是什么

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

ScrollView有什么用

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

ScrollView怎么用

1. 布局中加入ScrollView控件

	<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>

运行效果:

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)来达到一样的效果。

本篇源码下载地址:https://pan.baidu.com/s/1rDQY9ALQ14fv05RPquOPAw 提取码: helu

------转载请注明出处,感谢您对原创作者的支持------

有偿提供技术支持、Bug修复、项目外包、毕业设计、大小作业

Android学习小站

Q Q:1095817610

微信:jx-helu

邮箱:1095817610@qq.com

添加请备注"Android学习小站"