使用 scrollview 来使内容滑动。
去除滑动条
android:scrollbars="none"
使用 scrollview 包住布局内容
<ScrollView
android:id="@+id/scrollView"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
>
<!-- 包住什么样的布局没有限制-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!-- 控件内容-->
</LinearLayout>
</ScrollView>
实例
<?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="wrap_content"
tools:context=".ui.context.ContextFragment">
<ScrollView
android:id="@+id/scrollView1"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
>
<!-- 包住什么样的布局没有限制-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#39A8DF"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#39DFDF"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#39A8DF"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#39DFDF"
/>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>