zoukankan      html  css  js  c++  java
  • android开发学习笔记系列(3)--ScrollView与HorizontalScrollView

    ScrollView与HorizontalScrollView

    这是一个滚动视图,就是说如果你在你的UI中容不下那么多的内容,且你对自己的UI都已经设置好了px,OK,那么在适应屏幕过程中,我们并不希望使之压缩,于是我们可以采用ScrollView这个空间来包裹!

    注意,值得一提的是,它只能包裹一个组件,因此,你必须在这个过程中只能使用一个Layout来布局哦!

    当然上面所说的是,是垂直方向上的布局,如果是要采用水平布局的话,那么你可以采用HorizontalScrollView!

    详细介绍以上两个控件

    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的使用。

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

    布局文件xml:

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

    效果图:

    HorizontalScrollView

    对于HorizontalScrollView而言,其实所有的思想都与ScrollView类似,唯一的区别是HorizontalScrollView是支持水平滚动的。在上面的实例中,只需要改变一下外围的ScrollView为HorizontalScrollView,再把其中包裹的LinearLayout的android:orientation属性设置为horizontal即可实现水平滚动的效果。

    以上是借用了园友的博客,因为这个实在是没必要详细地进行介绍,在此非常感谢那位园友的分享,在这里,特意贴出以上资源的来源-------Android--UI之ScrollView

    如果有人非说我无耻拿园友的博客转载的话,在此表示深深的歉意,我承认转载了!但我仍然会遵循自己的写文原则:分享自己想分享的,写自己想写的,引用必写明原处,不辜负任何一位愿意出来分享的人,让大家都能和平地学习与工作!

  • 相关阅读:
    YOLO2 (2) 测试自己的数据
    Ubuntu 14.04服务器配置 (1) 安装和配置
    window10+linux双系统安装
    机械纪元 尼奥
    如何标数据
    usb-cam (3)摄像机标定文件-ORB-SLAM标定文件
    ORB-SLAM2(3) ROS下实时跑ORB_SLAM2
    usb-cam(1)安装
    usb-cam (2)摄像机标定
    Linux下的压缩zip,解压缩unzip命令详解及实例
  • 原文地址:https://www.cnblogs.com/samuelwnb/p/4325028.html
Copyright © 2011-2022 走看看