滚动视图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>
相关推荐
撩得Android一次心动14 分钟前
Android Navigation 组件全面讲解
android·jetpack·navigation
向阳是我18 分钟前
Flutter Android 编译错误修复:JVM Target Compatibility 不一致问题记录
android·jvm·flutter
Kapaseker22 分钟前
我想让同事知道我很懂 Compose 怎么办?
android·kotlin
小肝一下28 分钟前
3. 数据类型
android·数据库·mysql·adb
a2591748032-随心所记29 分钟前
android拆解super.img内容
android·linux·运维·服务器
Mr_pyx33 分钟前
MySQL性能优化:深入理解索引原理与查询优化实战
android
恋猫de小郭35 分钟前
Flutter 凉了没?Flutter 2026 的未来行程和规划,一些有趣的变化
android·前端·flutter
帅次37 分钟前
Android 高级工程师专题深挖:WebView、Context 与初始化链
android·binder·webview·zygote·web app·dalvik
y小花39 分钟前
安卓音频低延时与AAudio
android·音视频
Jwest202140 分钟前
佳维视工业安卓一体机在医生移动查房车中的应用
android