Day19 Android - NestedScrollView

今天主要要来简单的使用NestedScrollView这个应用,与ScrollView相似,也与昨天提到的RecyclerView一样都拥有能滚动的动作,NestedScrollView很常拿来用在版面做滚动,如果我设计的东西超出了NestedScrollView的版面大小,他就可以去进行滚动的动作,而在里面嵌入RecyclerView、或者在旁边设置toolbar也都是常见的应用,那麽接着就来简单的设计NestedScrollView并且观察。
https://ithelp.ithome.com.tw/upload/images/20210908/20139259CDnTkLpAzB.png
NestedScrollView没有需要在gradle/app添加依赖,但是如果发现元件旁边有类似下载的图案,则需要点击图案将此元件的dependency进行加入的动作(OK),然後就可以做使用了,接着我们先拉入一个NestedScrollView进入版面中,并且在(res.values.)strings.xml定义我等等想要显示的内容。

版面

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <!--要设计的东西-->
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

Strings.xml我定义了一个name为text的字串,我在里面放入了一些假文产生器的文字一千字,因为实在有点长我就不贴里面的内容了,大约是在string.xml写一个名为text的字串:

<resources>
<string name="text">...</string>
</resources>

接着我们就把想显示的元件放入NestedScrollView的LinearLayout中:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="68dp"
                android:text="NestScrollView"
                android:textSize="40dp"
                android:layout_gravity="center"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/text" />

            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button" />

        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

这样就可以使NestedScrollView做滚动了!简易应用。


成果:

https://ithelp.ithome.com.tw/upload/images/20210908/20139259SjzdT4Y3Lg.jpg


https://ithelp.ithome.com.tw/upload/images/20210908/201392594uiYVyAgA2.jpg


<<:  Day19 Let's ODOO: Logging

>>:  Kotlin Android 第29天,从 0 到 ML - TensorFlow Lite - 艺术风格转换(Style Transfer)

第11章:控制背景运行服务介绍

前言 在上一章节中,介绍了有关於Linux作业系统上的process程序管理,以及取得主机上的CPU...

GitHub Branch 策略 - 哪一种方式适合你?

若您对於 Git 相当熟悉,你应该对於建立分支(Branch) 应该不陌生。依据 GitHub 官方...

中阶魔法 - Callback Function

前情提要 「咦,今天早上开始就一直感觉有人在叫我,是艾草你吗?」 艾草:「没有呀,我怎麽没听到,确定...

30天零负担轻松学会制作APP介面及设计【DAY 14】

大家好,我是YIYI,今天我要来制作日记部分的介面。 如何进入日记页面? 日记的部分是点选像是笔的i...

Day3:Security Group 简介与布建

在AWS SA间流传着一句俗谚:「SG/NACL锁的好,资安没烦恼」。很多在用AWS的用户常常分不清...