Android--UI之ScrollView

简介:

前言

  本篇博客主要讲解ScrollView和HorizontalScrollView两个容器的使用。它们分别代表了垂直滚动以及水平滚动,滚动的内容是它其中包含的View。在本篇会简单介绍ScrollView和HorizontalScrollView的使用以及注意事项,最后以一个简单的Demo来演示一下这两个容器的使用。

ScrollView

  ScrollView,通过官方文档的继承关系可以看出,它继承自FrameLayout,所以它是一种特殊类型的FrameLayout,因为它可以使用用户滚动显示一个占据的空间大于物理显示的视图列表。值得注意的是,ScrollView只能包含一个子视图或视图组,在实际项目中,通常包含的是一个垂直的LinearLayout。

  值得注意的是,ScrollView不能和ListView一起使用,因为ListView已经对垂直方向的滚动做了处理,它会迫使如果ListView的内容大于物理视图的内容的时候,强制垂直滚动的效果,所以这里使用ScrollView和ListView混合使用是没有意义的,对于ListView的讲解,可以参见我的另外一篇博客:Android--UI之ListView。ScrollView还需要注意EditText自带的多行输入的滚动效果,也是不可以混合使用的,如果在ScrollView中包含了多行的EditText,那EditText中自带的滚动效果将失效。其中心思想就是ScrollView是一个滚动视图的容器,对于一些自带了滚动效果的控件,是无法和它一起被混合使用的。

  在Android平台下,与ScrollView类似的还有一个HorizontalScrollView容器,这个容器与ScrollView的作用相反,主要适用于水平滚动,了解了ScrollView就基本上了解了HorizontalScrollView,所以这里着重讲解ScrollView的使用。

示例Demo

  ScrollView其实就是一个布局,所以基本上没有什么太多的自己的方法或者属性需要特别讲解。这里直接展示一个Demo来讲解一下使用以及效果即可,这里提供了十张图片,需要放置在res/drawable-hdpi目录下。

  布局代码:

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <LinearLayout
 7         android:layout_width="match_parent"
 8         android:layout_height="wrap_content"
 9         android:orientation="vertical" >
10 
11         <TextView
12             android:layout_width="match_parent"
13             android:layout_height="wrap_content"
14             android:text="垂直滚动视图"
15             android:textSize="30dp" />
16 
17         <ImageView
18             android:layout_width="match_parent"
19             android:layout_height="wrap_content"
20             android:src="@drawable/bmp1" />
21 
22         <ImageView
23             android:layout_width="match_parent"
24             android:layout_height="wrap_content"
25             android:src="@drawable/bmp2" />
26 
27         <ImageView
28             android:layout_width="match_parent"
29             android:layout_height="wrap_content"
30             android:src="@drawable/bmp3" />
31 
32         <EditText
33             android:maxLines="2"
34             android:layout_width="match_parent"
35             android:layout_height="40dp" />
36 
37         <ImageView
38             android:layout_width="match_parent"
39             android:layout_height="wrap_content"
40             android:src="@drawable/bmp4" />
41 
42         <ImageView
43             android:layout_width="match_parent"
44             android:layout_height="wrap_content"
45             android:src="@drawable/bmp5" />
46 
47         <ImageView
48             android:layout_width="match_parent"
49             android:layout_height="wrap_content"
50             android:src="@drawable/bmp6" />
51 
52         <ImageView
53             android:layout_width="match_parent"
54             android:layout_height="wrap_content"
55             android:src="@drawable/bmp7" />
56 
57         <ImageView
58             android:layout_width="match_parent"
59             android:layout_height="wrap_content"
60             android:src="@drawable/bmp8" />
61 
62         <ImageView
63             android:layout_width="match_parent"
64             android:layout_height="wrap_content"
65             android:src="@drawable/bmp9" />
66 
67         <ImageView
68             android:layout_width="match_parent"
69             android:layout_height="wrap_content"
70             android:src="@drawable/bmp10" />
71     </LinearLayout>
72 
73 </ScrollView>
复制代码

  效果展示:

  

HorizontalScrollView

  对于HorizontalScrollView而言,其实所有的思想都与ScrollView类似,唯一的区别是HorizontalScrollView是支持水平滚动的。在上面的实例中,只需要改变一下外围的ScrollView为HorizontalScrollView,再把其中包裹的LinearLayout的android:orientation属性设置为horizontal即可实现水平滚动的效果。因为没有什么新的技术含量,这里就不再展示Demo代码了。

  效果展示:

  源码下载

总结

  对于现在的Android开发,大部分应用中,需要用到滚动效果的时候,比如说滑动的展示新闻的效果,都会直接使用ListView来装载数据。但是ScrollView还是有一定用处的,比如一些软件的属性的设置,就可以放在一个ScrollView中。核心思想就是对于一些动态的效果展示,就使用ListView,对于固定的一些效果展示,就使用ScrollView包裹即可。



本文转自承香墨影博客园博客,原文链接:http://www.cnblogs.com/plokmju/p/android_ScrollView.html,如需转载请自行联系原作者


相关文章
|
20天前
|
消息中间件 安全 数据处理
Android为什么不能在子线程更新UI
Android为什么不能在子线程更新UI
25 0
|
3月前
|
Android开发 开发者
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
42 1
|
3月前
|
开发工具 Android开发 开发者
Android UI设计: 解释Android的Nine-Patch图像是什么,它用于什么目的?
Android UI设计: 解释Android的Nine-Patch图像是什么,它用于什么目的?
31 4
|
3月前
|
API Android开发 开发者
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
31 2
|
3月前
|
Android开发 容器
Android UI设计: 什么是View和ViewGroup?
Android UI设计: 什么是View和ViewGroup?
36 0
|
6月前
|
设计模式 测试技术 vr&ar
提升你的Android开发技能:从AR/VR沉浸到UI设计和故障排除(三)
提升你的Android开发技能:从AR/VR沉浸到UI设计和故障排除
|
6月前
|
人工智能 机器人 区块链
提升你的Android开发技能:从AR/VR沉浸到UI设计和故障排除(二)
提升你的Android开发技能:从AR/VR沉浸到UI设计和故障排除
|
3月前
|
数据可视化 Android开发 容器
Android UI设计: 请解释LinearLayout、RelativeLayout和ConstraintLayout的区别。
Android UI设计: 请解释LinearLayout、RelativeLayout和ConstraintLayout的区别。
93 5
|
3月前
|
API Android开发
Google I/O :Android Jetpack 最新变化(三)UI
Google I/O :Android Jetpack 最新变化(三)UI
49 0
|
9天前
|
编解码 Android开发 UED
安卓UI/UX设计原则:打造引人入胜的用户体验
【4月更文挑战第13天】本文探讨了安卓UI/UX设计的关键原则,包括一致性、简洁性、反馈、清晰性、效率和适应性。一致性要求视觉和行为保持一致,利用系统UI;简洁性减少用户行动,简化导航;反馈需即时且明确;清晰性强调表达清晰,布局有序;效率关注性能优化和任务简化;适应性涉及多设备适配和用户多样性。遵循这些原则,可创建出色应用,提供无缝用户体验。设计应持续迭代,适应技术发展和用户需求。

热门文章

最新文章