滚动视图顶部的静态按钮 android studio

Static buttons over top a scroll view android studio

我想创建一个 activity,其中我在屏幕底部有 2 个按钮,在它们上方有一个滚动视图。在滚动视图中,我想要一个下拉菜单,用户可以在其中 select 进行一系列练习。如何将滚动视图放在按钮上方并让它们在滚动时保持在同一位置?

代码 activity

<?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:id="@+id/ConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WorkoutsCreater">


<TextView
    android:id="@+id/NameWorkoutTextView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:gravity="center"
    android:text="Name of Workout:"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    android:textSize="15dp"
    app:layout_constraintBottom_toBottomOf="@+id/NameWorkoutInput"
    app:layout_constraintEnd_toStartOf="@+id/NameWorkoutInput"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/NameWorkoutInput" />

<TextView
    android:id="@+id/nameExercise1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="20dp"
    android:gravity="center"
    android:text="Exercise 1:"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    android:textSize="15dp"
    app:layout_constraintBottom_toBottomOf="@id/spinnerExercise1"
    app:layout_constraintEnd_toStartOf="@+id/NameWorkoutInput"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/spinnerExercise1" />

<EditText

    android:id="@+id/NameWorkoutInput"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginEnd="40dp"
    android:layout_marginRight="40dp"
    android:inputType="text"
    android:maxLength="20"
    android:textSize="15dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/NameWorkoutTextView"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/BtnNew"
    android:layout_width="77dp"
    android:layout_height="77dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    android:background="@drawable/circle_plus"
    android:clickable="true"
    android:focusable="true"
    android:foreground="@drawable/ic_add_white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:srcCompat="@drawable/ic_add_white" />

<Button
    android:id="@+id/BtnSave"
    android:layout_width="77dp"
    android:layout_height="77dp"
    android:layout_marginStart="16dp"
    android:layout_marginBottom="16dp"
    android:background="@drawable/rounded_corners"
    android:foreground="@drawable/ic_save_white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<Spinner
    android:id="@+id/spinnerExercise1"
    android:layout_width="175dp"
    android:layout_height="43dp"
    android:layout_marginTop="50dp"
    android:layout_marginEnd="40dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/NameWorkoutInput" />

activity

的图片

如果你想添加一个停留在两个按钮上方的滚动视图,你可以在constraintlayout中添加一个NestedScrollview

<?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:id="@+id/ConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WorkoutsCreater">

    <androidx.core.widget.NestedScrollView
        android:layout_width="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/BtnNew"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/NameWorkoutTextView"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginStart="20dp"
                android:layout_marginLeft="20dp"
                android:gravity="center"
                android:text="Name of Workout:"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textSize="15dp"
                app:layout_constraintBottom_toBottomOf="@+id/NameWorkoutInput"
                app:layout_constraintEnd_toStartOf="@+id/NameWorkoutInput"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/NameWorkoutInput" />

            <TextView
                android:id="@+id/nameExercise1"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginStart="20dp"
                android:gravity="center"
                android:text="Exercise 1:"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textSize="15dp"
                app:layout_constraintBottom_toBottomOf="@id/spinnerExercise1"
                app:layout_constraintEnd_toStartOf="@+id/NameWorkoutInput"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/spinnerExercise1" />

            <EditText
                android:id="@+id/NameWorkoutInput"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:layout_marginEnd="40dp"
                android:layout_marginRight="40dp"
                android:inputType="text"
                android:maxLength="20"
                android:textSize="15dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/NameWorkoutTextView"
                app:layout_constraintTop_toTopOf="parent" />

            <Spinner
                android:id="@+id/spinnerExercise1"
                android:layout_width="175dp"
                android:layout_height="43dp"
                android:layout_marginTop="50dp"
                android:layout_marginEnd="40dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/NameWorkoutInput" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>


    <Button
        android:id="@+id/BtnNew"
        android:layout_width="77dp"
        android:layout_height="77dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:background="@drawable/ic_add_circle"
        android:clickable="true"
        android:focusable="true"
        android:foreground="@drawable/ic_add_white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_add_white" />

    <Button
        android:id="@+id/BtnSave"
        android:layout_width="77dp"
        android:layout_height="77dp"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="16dp"
        android:background="@drawable/ic_remove_red_ba0a00"
        android:foreground="@drawable/ic_save_white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

免责声明:嵌套布局比平面布局成本更高

请看这个https://developer.android.com/topic/performance/rendering/optimizing-view-hierarchies