Android-UI之ScrollView

前言

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

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目录下。

  布局代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="垂直滚动视图"
            android:textSize="30dp" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp1" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp2" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp3" />

        <EditText
            android:maxLines="2"
            android:layout_width="match_parent"
            android:layout_height="40dp" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp4" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp5" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp6" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp7" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp8" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp9" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/bmp10" />
    </LinearLayout>

</ScrollView>

效果展示:

HorizontalScrollView

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

  效果展示:

总结

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

作者: 承香墨影

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值