• 【Android Studio】常用布局 --- 滚动视图ScrollView


    问题引入:手机屏幕的显示空间有限,常常需要上下滑动或左右滑动才能拉出其余页面内容,可惜一般的布局节点 都不支持自行滚动,这时就要借助滚动视图了。与线性布局类似,滚动视图也分为垂直方向和水平方向 两类,其中垂直滚动视图名为ScrollView,水平滚动视图名为

    HorizontalScrollView

    这两个滚动视图的 使用并不复杂,

    主要注意以下3点:

    (1)垂直方向滚动时,layout_width属性值设置为match_parent,layout_height属性值设置为wrap_content。

    (2)水平方向滚动时,layout_width属性值设置为wrap_content,layout_height属性值设置为match_parent。

    (3)滚动视图节点下面必须且只能挂着一个子布局节点,否则会在运行时报错。

    Caused by:java.lang.IllegalStateException:ScrollView can host only one direct child
    

    下面是垂直滚动视图ScrollView和水平滚动视图HorizontalScrollView的XML例子:

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2.    android:layout_width="match_parent"
    3.    android:layout_height="match_parent"
    4.    android:orientation="vertical">
    5.    
    6.    <HorizontalScrollView
    7.        android:layout_width="wrap_content"
    8.        android:layout_height="200dp">
    9.        
    10.        <LinearLayout
    11.            android:layout_width="wrap_content"
    12.            android:layout_height="match_parent"
    13.            android:orientation="horizontal">
    14.            <View
    15.                android:layout_width="300dp"
    16.                android:layout_height="match_parent"
    17.                android:background="#aaffff" />
    18.            <View
    19.                android:layout_width="300dp"
    20.                android:layout_height="match_parent"
    21.                android:background="#ffff00" />
    22.        LinearLayout>
    23.    HorizontalScrollView>
    24.  
    25.    <ScrollView
    26.        android:layout_width="match_parent"
    27.        android:layout_height="wrap_content">
    28.        
    29.        <LinearLayout
    30.            android:layout_width="match_parent"
    31.            android:layout_height="wrap_content"
    32.            android:orientation="vertical">
    33.            <View
    34.                android:layout_width="match_parent"
    35.                android:layout_height="400dp"
    36.                android:background="#00ff00" />
    37.            <View
    38.                android:layout_width="match_parent"
    39.                android:layout_height="400dp"
    40.                android:background="#ffffaa" />
    41.        LinearLayout>
    42.    ScrollView>
    43. LinearLayout>

    运行测试App,可知ScrollView在纵向滚动,而HorizontalScrollView在横向滚动。 有时ScrollView的实际内容不够,又想让它充满屏幕,怎么办呢?如果把layout_height属性赋值为match_parent,结果还是不会充满,正确的做法是再增加一行属性android:fillViewport(该属性为true表示允许填满视图窗口),属性片段举例如下:

    1. android:layout_height="match_parent"
    2. android:fillViewport="true"

    运行结果:

    注:运行前修改清单文件

    滑动页面

    感谢观看!!!

  • 相关阅读:
    Codeforces暑期训练周报(7.28~8.3)
    Fourier分析导论——第4章——Fourier级数的一些应用(E.M. Stein & R. Shakarchi)
    快速教你如何搭建数据驱动自动化测试框架?
    lintcode 1840 · 矩阵还原【中等 vip 二维前缀和数组】
    Linux mkdir命令:创建目录(文件夹)
    【OpenDDS开发指南V3.20】第四章:条件和监听
    防火墙用户管理理论+实验
    家政服务接单小程序开发源码 家政保洁上门服务小程序源码 开源完整版
    在Winform分页控件中集成导出PDF文档的功能
    【数据结构基础_字符串】Leetcode 763.划分字母区间
  • 原文地址:https://blog.csdn.net/qq_64976935/article/details/127881626