Android超详细讲解组件ScrollView的使用


Posted in Java/Android onMarch 31, 2022

概述

    ScrollView也是一个容器,它是FrameLayout的子类,它的主要作用就是将超出物理屏幕的内容显示出来,(就是滚动条效果)ScrollView提供垂直滚动,进而可将超出物理屏幕的内容显示出来。

      在一般情况下,可以将一个采用垂直方式布局组件的LinearLayout作为ScrollLayout容器的子组件,同时,在LinearLayout容器中可以显示超出屏幕物理高度的内容。

练习

这么说有点抽象,然后我们现在实现完成一个调试板颜色的显示,效果如下:(可以往下滚动)

代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#07C2FB" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#07C2FB" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#C60426FD" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#C60426FD" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#032898" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#032898" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#021173" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#021173" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#504DAE" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#504DAE" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#3C60A6" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#3C60A6" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#282EA8" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#282EA8" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#273523" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="##5E7559" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#0E1E73" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#0E1E73" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#305BC8" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#305BC8" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#282EA8" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#282EA8" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#0B685E" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#0B685E" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#263B7E" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#263B7E" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <View
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#0A7AC5" />
            <TextView
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#0A7AC5" />
        </LinearLayout>
 
    </LinearLayout>
 
</ScrollView>

Android超详细讲解组件ScrollView的使用

      当然了ScrollView只提供了垂直滚动条,若要使用水平滚动功能,则Android提供了HorizontalScrollView容器,HorizontalScrollView容器可以提供水平滚动,它的使用方法与ScrollView类似

HorizontalScrollView:

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
 
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal">
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <View
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#2479CE" />
 
            <TextView
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#2479CE" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <View
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#C97A06" />
 
            <TextView
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#C97A06" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <View
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#68060E" />
 
            <TextView
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#68060E" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <View
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#271904" />
 
            <TextView
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#271904" />
        </LinearLayout>
 
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <View
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#673AB7" />
 
            <TextView
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#673AB7" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <View
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#1B5834" />
 
            <TextView
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#1B5834" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <View
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#680379" />
 
            <TextView
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#680379" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <View
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#043485" />
 
            <TextView
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#043485" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <View
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#798506" />
 
            <TextView
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#798506" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <View
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#B12E04" />
 
            <TextView
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#B12E04" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <View
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#A09006" />
 
            <TextView
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#A09006" />
        </LinearLayout>
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <View
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="2"
                android:background="#920C02" />
 
            <TextView
                android:layout_width="200px"
                android:layout_height="60dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="#920C02" />
        </LinearLayout>
 
 
    </LinearLayout>
 
</HorizontalScrollView>

 实现效果:

Android超详细讲解组件ScrollView的使用

到此这篇关于Android超详细讲解组件ScrollView的使用的文章就介绍到这了,更多相关Android ScrollView内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Java/Android 相关文章推荐
Spring Bean的实例化之属性注入源码剖析过程
Jun 13 Java/Android
Java内存模型之happens-before概念详解
Jun 13 Java/Android
Java实现简易的分词器功能
Jun 15 Java/Android
Java常用工具类汇总 附示例代码
Jun 26 Java/Android
SpringCloud的JPA连接PostgreSql的教程
Jun 26 Java/Android
SpringBoot+VUE实现数据表格的实战
Aug 02 Java/Android
SpringDataJPA在Entity中常用的注解介绍
Dec 06 Java/Android
解决persistence.xml配置文件修改存放路径的问题
Feb 24 Java/Android
Mybatis-Plus 使用 @TableField 自动填充日期
Apr 26 Java/Android
Java 多线程并发FutureTask
Jun 28 Java/Android
spring 项目实现限流方法示例
Jul 15 Java/Android
Java使用HttpClient实现文件下载
Aug 14 Java/Android
Spring Boot DevTools 全局配置学习指南
Spring事务管理下synchronized锁失效问题的解决方法
Mar 31 #Java/Android
Spring依赖注入多种类型数据的示例代码
Mar 31 #Java/Android
springboot layui hutool Excel导入的实现
spring注解 @PropertySource配置数据源全流程
Mar 25 #Java/Android
Netty客户端接入流程NioSocketChannel创建解析
Mar 25 #Java/Android
Java 超详细讲解设计模式之中的抽象工厂模式
You might like
简单的PHP留言本实例代码
2010/05/09 PHP
PHP使用GIFEncoder类处理gif图片实例
2014/07/01 PHP
使用Git实现Laravel项目的自动化部署
2019/11/24 PHP
基于jQuery实现的当离开页面时出现提示的实现代码
2011/06/27 Javascript
JS获取节点的兄弟,父级,子级元素的方法
2014/01/09 Javascript
javascript中几个容易混淆的概念总结
2015/04/14 Javascript
基于cssSlidy.js插件实现响应式手机图片轮播效果
2016/08/30 Javascript
基于Bootstrap的标签页组件及bootstrap-tab使用说明
2017/07/25 Javascript
使用cookie绕过验证码登录的实现代码
2017/10/12 Javascript
jQuery实现使用sort方法对json数据排序的方法
2018/04/17 jQuery
一个简单的node.js界面实现方法
2018/06/01 Javascript
基于vue实现移动端圆形旋钮插件效果
2018/11/28 Javascript
ES6 对象的新功能与解构赋值介绍
2019/02/05 Javascript
vue 详情跳转至列表页实现列表页缓存
2019/03/27 Javascript
Vue 自适应高度表格的实现方法
2020/05/13 Javascript
vue实现简易图片左右旋转,上一张,下一张组件案例
2020/07/31 Javascript
解决Vue大括号字符换行踩的坑
2020/11/09 Javascript
Python基于回溯法子集树模板解决选排问题示例
2017/09/07 Python
Linux下python3.6.1环境配置教程
2018/09/26 Python
python查询文件夹下excel的sheet名代码实例
2019/04/02 Python
如何在Python中实现goto语句的方法
2019/05/18 Python
Python 进程操作之进程间通过队列共享数据,队列Queue简单示例
2019/10/11 Python
基于Python实现扑克牌面试题
2019/12/11 Python
Python实现投影法分割图像示例(二)
2020/01/17 Python
TensorBoard 计算图的可视化实现
2020/02/15 Python
如何使用Python处理HDF格式数据及可视化问题
2020/06/24 Python
python else语句在循环中的运用详解
2020/07/06 Python
解决H5的a标签的download属性下载service上的文件出现跨域问题
2019/07/16 HTML / CSS
联想加拿大官方网站:Lenovo Canada
2018/04/05 全球购物
香港百佳网上超级市场:PARKNSHOP.com
2020/06/10 全球购物
机械设计职业生涯规划书
2013/12/27 职场文书
初中班主任寄语
2014/04/04 职场文书
关于雷锋的演讲稿
2014/05/10 职场文书
2014全年工作总结
2014/11/27 职场文书
文艺晚会开场白
2015/05/29 职场文书
python 下载文件的几种方式分享
2021/04/07 Python