前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >屏幕宽高不够,滚动视图ScrollView来凑

屏幕宽高不够,滚动视图ScrollView来凑

作者头像
分享达人秀
发布2018-02-05 12:02:10
3K0
发布2018-02-05 12:02:10
举报
文章被收录于专栏:分享达人秀分享达人秀

前面几期学习了ProgressBar系列组件、ViewAnimator系列组件、Picker系列组件和时间日期系列组件,接下来几期继续来学习常见的其他组件。

一、ScrollView概述

从前面的学习有的同学可能已经发现,当拥有很多内容时屏幕显示不完,显示不全的部分完全看不见。但是在实际项目里面,很多内容都不止一个屏幕宽度或高度,那怎么办呢?那就需要本节学习的ScrollView来完成。

在默认情况下,ScrollView只是为其他组件添加垂直滚动条,如果应用需要添加水平滚动条,则可借助于另一个滚动视图HorizontalScrollView来实现。ScrollView与HorizontalScrollView的功能基本相似,只是前者添加垂直滚动条,后者添加水平滚动条。

ScrollView由FrameLayout派生而出,它就是一个用于为普通组件添加滚动条的组件。 ScrollView里最多只能包含一个组件,而ScrollView的作用就是为该组件添加垂直滚动条。

ScrollView支持的XML属性如下:

  • android:scrollX:以像素为单位设置水平方向滚动的的偏移值。
  • android:scrollY:以像素为单位设置垂直方向滚动的的偏移值。
  • android:scrollbarAlwaysDrawHorizontalTrack:设置是否始终显示垂直滚动条。
  • android:scrollbarAlwaysDrawVerticalTrack:设置是否始终显示垂直滚动条。
  • android:scrollbarDefaultDelayBeforeFade:设置N毫秒后开始淡化,以毫秒为单位。
  • android:scrollbarFadeDuration:设置滚动条淡出效果(从有到慢慢的变淡直至消失)时间,以毫秒为单位。
  • android:scrollbarSize:设置滚动条的宽度。
  • android:scrollbarStyle:设置滚动条的风格和位置。属性值有以下几个:
  • outsideInset:该ScrollBar显示在视图(view)的边缘,增加了view的padding. 如果可能的话,该ScrollBar仅仅覆盖这个view的背景。
  • outsideOverlay:该ScrollBar显示在视图(view)的边缘,不增加view的padding,该ScrollBar将被半透明覆盖。
  • insideInset:该ScrollBar显示在padding区域里面,增加了控件的padding区域,该ScrollBar不会和视图的内容重叠。
  • insideOverlay:该ScrollBar显示在内容区域里面,不会增加了控件的padding区域,该ScrollBar以半透明的样式覆盖在视图(view)的内容上。
  • android:scrollbarThumbHorizontal:设置水平滚动条的drawable。
  • android:scrollbarThumbVertical:设置垂直滚动条的drawable。
  • android:scrollbarTrackHorizontal:设置水平滚动条背景(轨迹)的色drawable。
  • android:scrollbarTrackVertical:设置垂直滚动条背景(轨迹)的drawable。
  • android:scrollbars:设置滚动条显示。属性值有:none、horizontal、vertical。

ScrollView的几个常用方法有:

  • addView (View child):添加子视图。如果事先没有给子视图设置layout参数,会采用当前ViewGroup的默认参数来设置子视图。
  • addView (View child, int index):添加子视图。如果事先没有给子视图设置layout参数,会采用当前ViewGroup的默认参数来设置子视图。
  • arrowScroll (int direction):响应点击上下箭头时对滚动条滚动的处理。
  • fling (int velocityY):滚动视图的滑动(fling)手势。

二、ScrollView示例

接下来通过一个简单的示例程序来学习ScrollView的使用。

继续使用WidgetSample工程的advancedviewsample模块,在app/main/res/layout/目录下创建scrollview_layout.xml文件,在其中填充如下代码片段:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="10dp"
            android:fillViewport="false">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@drawable/image" />
        <Button
            android:id="@+id/more_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="more" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ScrollView"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/description"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>
</ScrollView>

其中description为定义的字符串,由于内容较多,此处不在给出。

运行程序,可以看到下图所示界面效果,界面可以上下滚动。

关于ScrollView先学到这里,更多用法建议自己多加练习。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-09-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 分享达人秀 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档