滚动视图HorizontalScrollView和ScrollView

滚动视图分为水平滚动(HorizontalScrollView)和垂直滚动(ScrollView);

在默认情况下,当视图中的内容比较多而屏幕显示不下时,超出的部分无法看到,Android本身没有提供屏幕滚动的功能。如果需要滚动就需要使用滚动视图,滚动视图是帧布局(FrameLayout)的子类,所以在滚动视图中可以添加任何子视图。但是一个滚动视图只能放一个子视图。如果想要放多个子视图,就需要先放一个子布局管理器,在子布局管理器中在放置其他子视图。

使用参数:

在水平滚动(HorizontalScrollView)视图中,需要将宽度设置为自适应(wrap_content),或使用权重设置内容视图的宽度。

在垂直滚动(ScrollView)视图中,需要将高度设置为自适应(wrap_content),或使用权重设置内容视图的高度。

常见属性:

android:fillViewport:

<1>HorizontalScrollView:设置子视图是否填充HorizontalScrollView的可视区域。默认值为true。

<2>ScrollView:设置子视图是否填充ScrollView的可视区域。设置为true表示内容将充满整个ScrollView,默认为false。

android:overScrollMode:设置滚动边界效果模式

="always"(总是显示边界阴影效果)

="never"(永不显示边界阴影效果)

="ifContentScrolls"(仅当内容发生滚动时显示边界阴影效果)

android:scrollbars:设置滚动条的显示方式

="horizontal"(只显示水平滚动条)

="vertical"(只显示垂直滚动条)

="none"(不显示滚动条)

android:scrollbarStyle:自定义滚动条的风格

="default"(系统默认风格)

="insideInset"(滚动条在内部偏移位置显示)

="outsideInset"(滚动条在外部偏移位置显示)

常用方法:

scrollTo(int x, int y):滚动到指定的坐标位置。

其中x表示水平方向上的滚动位置,y表示垂直方向上的滚动位置。

smoothScrollTo(int x, int y):平滑地滚动到指定的坐标位置。

与scrollTo()相比,该方法会有一个过渡效果,使得滚动更加平滑。

fullScroll(int direction):滚动到指定方向的边界。

=View.FOCUS_LEFT(滚动到最左边)

=View.FOCUS_RIGHT(滚动到最右边)

=View.FOCUS_FORWARD(按照指定方向进行滚动)

computeHorizontalScrollRange():获取水平滚动范围的总长度。

setSmoothScrollingEnabled(boolean enabled):设置是否启用平滑滚动效果。

onScrollChanged(int l, int t, int oldl, int oldt):当滚动位置发生变化时被调用的回调方法。

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!--这是一个水平滚动条-->
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:fadeScrollbars="false">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ff0000"
                android:text="这是一个水平滚动条"
                android:textSize="60dp" />
        </LinearLayout>
    </HorizontalScrollView>
    <!--这是一个垂直滚动进度条-->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fadeScrollbars="false"
        android:overScrollMode="ifContentScrolls">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#fff000"
                android:text="这\n是\n一\n个\n垂\n直\n滚\n动\n条"
                android:textSize="60dp" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>
相关推荐
_祝你今天愉快8 小时前
从驱动来分析服务的添加过程
android
Dovis(誓平步青云)10 小时前
折叠屏悬停看视频,上半屏和下半屏应该各做什么
android·java·服务器·开发语言·安全·音视频
alexhilton11 小时前
让架构边界变成可执行的测试
android·kotlin·android jetpack
hai_android13 小时前
SendChannel 与 ReceiveChannel 通信机制
android
数据治理自习室15 小时前
AI 应用评测体系(GraphRAG)
android·大数据·人工智能·kotlin
爱笑鱼15 小时前
Android 系统启动机制(五):system_server 是 init 启动的,还是 Zygote fork 出来的?
android
智购科技无人售货机工厂16 小时前
2026自动售货机防拆机物理安全设计:从安全螺丝到结构互锁的工程实践~YH
android·网络·驱动开发·python·单片机·安全·云原生
开开心心就好16 小时前
电子教鞭工具支持画框写字插图片功能齐全
android·开发语言·前端·javascript·人工智能·pdf·html
Dovis(誓平步青云)16 小时前
拍视频前先把镜头想清楚:做一个分镜取景辅助器
android·java·服务器·javascript·人工智能
峥嵘life18 小时前
Android16 系统 APEX 模块说明
android·大数据·开发语言