滚动视图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>
相关推荐
有味道的男人44 分钟前
Open Claw对接1688平台
android·rxjava
_李小白2 小时前
【android opencv学习笔记】Day 17: 目标追踪(MeanShift)
android·opencv·学习
用户86022504674723 小时前
AI 分析头部APP系统优化框架
android
用户86022504674723 小时前
AI分析头部APP优化框架
android
2501_916007475 小时前
iOS开发中抓取HTTPS请求的完整解决方法与步骤详解
android·网络协议·ios·小程序·https·uni-app·iphone
lvronglee8 小时前
【数字图传第四步】Android App查看图传视频
android·音视频
90后的晨仔8 小时前
Android 程序入口与核心组件详解
android
90后的晨仔8 小时前
Kotlin 简介与开发环境搭建
android
BU摆烂会噶9 小时前
【LangGraph】House_Agent 实战(四):预定流程 —— 中断与人工干预
android·人工智能·python·langchain
AI玫瑰助手9 小时前
Python运算符:比较运算符(等于不等等于大于小于)与返回值
android·开发语言·python