• 【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"

    运行结果:

    注:运行前修改清单文件

    滑动页面

    感谢观看!!!

  • 相关阅读:
    Spring Boot+Vue3前后端分离实战wiki知识库系统之前后端交互整合
    java同步方法
    什么是分布式软件系统
    【Python工程师之高性能爬虫】
    测试工程师通常用哪个单元测试库来测试Java程序?
    leetcode刷题:栈与队列06(前 K 个高频元素)
    FPGA学习专栏-串口通信(xinlinx)
    云计算的openStack 究竟是为了解决什么问题?一句话说清楚
    Spring cloud stream实现Kafka的消息收发
    开源B2B网站电子商务平台源码下载搭建 实现高效交易的桥梁
  • 原文地址:https://blog.csdn.net/qq_64976935/article/details/127881626